Creates a SHA-256 message digest in Byte
array format
from a message in Byte
array format.
Public Declare Function SHA2_BytesHash Lib "diCryptoSys.dll"
(ByRef lpDigest As Byte, ByRef lpData As Byte, ByVal nDataLen As Long) As Long
nRet = SHA2_BytesHash(abDigest(0), abData(0), nDataLen)
' Note the "(0)" after the byte array parameters
long __stdcall SHA2_BytesHash(unsigned char *lpDigest, const unsigned char *lpData, long nDataLen);
If successful, the return value is 0; otherwise it returns a non-zero error code.
This function differs from other SHA2 functions in that it creates a digest in Byte
array
format. This is particularly useful when you need to carry out repeated hash operations.
lpDigest must be at least 32 (API_MAX_SHA2_BYTES) bytes long.
Dim nRet As Long
Dim abData(2) As Byte ' Create 3-byte array (NB zero-based)
Dim abDigest(31) As Byte ' Create 32-byte array to receive digest
' Setup byte array with "abc"
abData(0) = Asc("a")
abData(1) = Asc("b")
abData(2) = Asc("c")
' Compute SHA-256 hash digest
nRet = SHA2_BytesHash(abDigest(0), abData(0), 3)
' Print resulting bytes in hex format
Debug.Print nRet; cnvHexStrFromBytes(abDigest)
This should result in output as follows:
0 BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD
This second example shows how to carry out repeated hashes of the digest.
Dim nRet As Long Dim abData(2) As Byte ' Create 3-byte array (NB zero-based) Dim abDigest(31) As Byte ' Create 32-byte array to receive digest Dim i As Integer ' Setup byte array with "abc" abData(0) = Asc("a") abData(1) = Asc("b") abData(2) = Asc("c") ' Compute SHA-256 hash digest of input nRet = SHA2_BytesHash(abDigest(0), abData(0), 3) ' Now carry out repeated hashes of the digest For i = 2 To 1000 nRet = SHA2_BytesHash(abDigest(0), abDigest(0), 32) Next ' Print H(1000) in hex format Debug.Print cnvHexStrFromBytes(abDigest)
This should result in output as follows:
FC8A6B86A13F71CD9A67F558AB6FD82A3DD89186163A017ED8051ACF6D3F8F99