1
0
mirror of https://e.coding.net/circlecloud/VBFunctionBas.git synced 2024-12-27 07:58:48 +00:00

添加MD5模块...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-11-16 21:10:29 +08:00
parent 648e8d7a04
commit 7f1cfb17fd

65
MD5Bas.bas Normal file
View File

@ -0,0 +1,65 @@
Attribute VB_Name = "MD5Bas"
Option Explicit
Option Base 0
Public Type MD5_CTX
i(1) As Long
buf(3) As Long
inc(63) As Byte
digest(15) As Byte
End Type
Public Declare Sub MD5Init Lib "Cryptdll.dll" (ByVal pContex As Long)
Public Declare Sub MD5Final Lib "Cryptdll.dll" (ByVal pContex As Long)
Public Declare Sub MD5Update Lib "Cryptdll.dll" (ByVal pContex As Long, ByVal lPtr As Long, ByVal nSize As Long)
Public Function ConvBytesToBinaryString(bytesIn() As Byte) As String
Dim i As Long
Dim nSize As Long
Dim strRet As String
nSize = UBound(bytesIn)
For i = 0 To nSize
strRet = strRet & Right$("0" & Hex(bytesIn(i)), 2)
Next
ConvBytesToBinaryString = strRet
End Function
Public Function GetMD5Hash(bytesIn() As Byte) As Byte()
Dim ctx As MD5_CTX
Dim nSize As Long
nSize = UBound(bytesIn) + 1
MD5Init VarPtr(ctx)
MD5Update ByVal VarPtr(ctx), ByVal VarPtr(bytesIn(0)), nSize
MD5Final VarPtr(ctx)
GetMD5Hash = ctx.digest
End Function
Public Function GetMD5Hash_Bytes(bytesIn() As Byte) As String
GetMD5Hash_Bytes = ConvBytesToBinaryString(GetMD5Hash(bytesIn))
End Function
Public Function GetMD5Hash_String(ByVal strIn As String) As String
GetMD5Hash_String = GetMD5Hash_Bytes(StrConv(strIn, vbFromUnicode))
End Function
Public Function GetMD5Hash_File(ByVal strFile As String) As String
Dim lFile As Long
Dim Bytes() As Byte
Dim lSize As Long
If Dir(strFile) = "" Then
GetMD5Hash_File = ""
Exit Function
End If
lSize = FileLen(strFile)
If (lSize) Then
lFile = FreeFile
ReDim Bytes(lSize - 1)
Open strFile For Binary As lFile
Get lFile, , Bytes
Close lFile
GetMD5Hash_File = GetMD5Hash_Bytes(Bytes)
End If
End Function