Creates a message digest hash in byte format for a file.
VB6/VBA
Debug.Print "Testing HASH_File ..." Dim nRet As Long Dim abDigest() As Byte Dim sFileName As String ' File to be hashed contains a total of 13 bytes: "hello world" plus CR-LF ' 68 65 6c 6c 6f 20 77 6f 72 6c 64 0d 0a hello world.. sFileName = ".\hello.txt" ' Pre-dimension digest array - do this each time ReDim abDigest(API_MAX_HASH_BYTES) ' Create default hash (SHA1) in binary mode nRet = HASH_File(abDigest(0), API_MAX_HASH_BYTES, sFileName, 0) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest) ' Use SHA1 in "text" mode ReDim abDigest(API_MAX_HASH_BYTES) nRet = HASH_File(abDigest(0), API_MAX_HASH_BYTES, sFileName, API_HASH_MODE_TEXT) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest) ' Use MD5 ReDim abDigest(API_MAX_HASH_BYTES) nRet = HASH_File(abDigest(0), API_MAX_HASH_BYTES, sFileName, API_HASH_MD5) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest) ' Use MD5 in "text" mode ReDim abDigest(API_MAX_HASH_BYTES) nRet = HASH_File(abDigest(0), API_MAX_HASH_BYTES, sFileName, API_HASH_MD5 Or API_HASH_MODE_TEXT) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest)
Output
Testing HASH_File ... 20 88A5B867C3D110207786E66523CD1E4A484DA697 20 22596363B3DE40B06F981FB85D82312E8C0ED511 16 A0F2A3C1DCD5B1CAC71BF0C03F2FF1BD 16 6F5902AC237024BDD0C176CB93063DC4
VB.NET
Console.WriteLine("Testing HASH_File ...")
''Dim nRet As Integer
Dim abDigest() As Byte
Dim sFileName As String
Dim strDigest As String
' File to be hashed contains a total of 13 bytes: "hello world" plus CR-LF
' 68 65 6c 6c 6f 20 77 6f 72 6c 64 0d 0a hello world..
sFileName = ".\hello.txt"
' Pre-dimension digest array - do this each time
''ReDim abDigest(API_MAX_HASH_BYTES)
' Create default hash (SHA1) in binary mode
abDigest = Hash.BytesFromFile(sFileName, HashAlgorithm.Sha1)
''If nRet > 0 Then ReDim Preserve abDigest(nRet - 1)
Console.WriteLine(abDigest.Length & " " & Cnv.ToHex(abDigest))
' Use SHA1 in "text" mode
' [VB.NET] Only option in v4.2 for a "text" file is to use HexFromTextFile
strDigest = Hash.HexFromTextFile(sFileName, HashAlgorithm.Sha1)
Console.WriteLine(strDigest.Length & " " & strDigest)
' Use MD5
''ReDim abDigest(API_MAX_HASH_BYTES)
abDigest = Hash.BytesFromFile(sFileName, HashAlgorithm.Md5)
''If nRet > 0 Then ReDim Preserve abDigest(nRet - 1)
Console.WriteLine(abDigest.Length & " " & Cnv.ToHex(abDigest))
' Use MD5 in "text" mode
strDigest = Hash.HexFromTextFile(sFileName, HashAlgorithm.Md5)
Console.WriteLine(strDigest.Length & " " & strDigest)
[Contents]