Creates a message digest hash as a byte array from byte data. The hash algorithm to use is passed in the options parameter.
Public Declare Function HASH_Bytes Lib "diCryptoSys.dll"
(ByRef lpDigest As Byte, ByVal nDigLen As Long, ByRef lpMessage As Byte,
ByVal nMsgLen As Long, ByVal nOptions As Long) As Long
nRet = HASH_Bytes(abDigest(0), nDigLen, abMessage(0), nMsgLen, nOptions)
' Note the "(0)" after the byte array parameters
long __stdcall HASH_Bytes(unsigned char *lpOutput, long nOutLen, const void *lpMessage, long nMsgLen, 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 hashBytes
(lpMessage() As Byte, nOptions As Long) As Byte()
static Hash.data(data, alg=Alg.SHA1)
Specify a zero nDigLen parameter to find out the required length of the output array.
The maximum possible length is API_MAX_HASH_BYTES
.
Hint: SHA-1 requires 20 bytes; MD5 and MD2 require 16 bytes; SHA-512 requires 64.
The final digest will be truncated to the specified length if less than the expected size.
Dim nRet As Long Dim abDigest() As Byte Dim abMessage() As Byte Dim nMsgLen As Long ' Set up message to be hashed abMessage = StrConv("abc", vbFromUnicode) nMsgLen = UBound(abMessage) + 1 ' Pre-dimension digest array (NB zero-based so subtract one) ReDim abDigest(API_MAX_HASH_BYTES - 1) ' Create default hash (SHA-1) nRet = HASH_Bytes(abDigest(0), API_MAX_HASH_BYTES, abMessage(0), nMsgLen, 0) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest) ' Repeat for MD5 ReDim abDigest(API_MAX_HASH_BYTES - 1) nRet = HASH_Bytes(abDigest(0), API_MAX_HASH_BYTES, abMessage(0), nMsgLen, API_HASH_MD5) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest) ' Repeat for SHA-3-384 ReDim abDigest(API_MAX_HASH_BYTES - 1) nRet = HASH_Bytes(abDigest(0), API_MAX_HASH_BYTES, abMessage(0), nMsgLen, API_HASH_SHA3_384) If nRet > 0 Then ReDim Preserve abDigest(nRet - 1) Debug.Print nRet, cnvHexStrFromBytes(abDigest)
The above example should produce the following output:
20 A9993E364706816ABA3E25717850C26C9CD0D89D 16 900150983CD24FB0D6963F7D28E17F72 48 EC01498288516FC926459F58E2C6AD8DF9B473CB0FC08C2596DA7CF0E49BE4B298D88CEA927AC7F539F1EDF228376D25
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))
HASH_HexFromBytes
HASH_HexFromHex