Creates a message digest hash in byte format for a file. The hash algorithm to use is passed in the options parameter.
Public Declare Function HASH_File Lib "diCryptoSys.dll"
(ByRef lpDigest As Byte, ByVal nDigLen As Long, ByVal strFileName As String,
ByVal nOptions As Long) As Long
nRet = HASH_File(abDigest(0), nDigLen, strFileName, nOptions)
long __stdcall HASH_File(unsigned char *lpOutput, long nOutLen, const char *szFileName, long nOptions);
If successful, the return value is the number of bytes in the hash digest array; otherwise it returns a negative error code.
Public Function hashFile
(szFileName As String, nOptions As Long) As Byte()
static Hash.file(filename, alg=Alg.SHA1)
Specify a zero nDigLen parameter to find out the required length of the output array.
The maximum size will be API_MAX_HASH_BYTES
.
The final digest will be truncated to the specified length if less than the expected size.
Add the option API_HASH_MODE_TEXT to work in "text" mode where CR-LF pairs are treated
as a single newline (LF) character.
Use this option to pass hash digests of text file between Windows and Unix systems.
The default mode is "binary" where each byte is treated individually.
Only ANSI file names and paths are supported (but see CNV_ShortPathName as a work around).
Note that Ascon-Hash is not available with this function.
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 = "C:\Test\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)
This should produce the following output:
20 88A5B867C3D110207786E66523CD1E4A484DA697 20 22596363B3DE40B06F981FB85D82312E8C0ED511 16 A0F2A3C1DCD5B1CAC71BF0C03F2FF1BD 16 6F5902AC237024BDD0C176CB93063DC4
Dim lpMessage() As Byte
lpMessage = StrConv("abc", vbFromUnicode)
Debug.Print "lpMessage=" & cnvHexStrFromBytes(lpMessage)
Debug.Print "OK:" & vbCrLf & "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
Debug.Print hashHexFromBytes(lpMessage, API_HASH_SHA256)
Debug.Print hashHexFromHex("616263", API_HASH_SHA256)
Debug.Print hashHexFromFile("abc.txt", API_HASH_SHA256)
Debug.Print cnvHexStrFromBytes(hashBytes(lpMessage, API_HASH_SHA256))
Debug.Print cnvHexStrFromBytes(hashFile("abc.txt", API_HASH_SHA256))