CryptoSys API Library Manual

HASH_Bytes

Creates a message digest hash as a byte array from byte data. The hash algorithm to use is passed in the options parameter.

VBA/VB6 Syntax

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

C/C++ Syntax

long __stdcall HASH_Bytes(unsigned char *lpOutput, long nOutLen, const void *lpMessage, long nMsgLen, long nOptions);

Parameters

lpOutput
[out] array to receive the hash digest.
nOutLen
[in] specifying the length in bytes of the output array.
lpMessage
[in] array containing the message data
nMsgLen
[in] specifying length of the message data in bytes.
nOptions
[in] Option flags. Select one of:
API_HASH_SHA1 (0) to use the SHA-1 algorithm (default)
API_HASH_SHA224 to use the SHA-224 algorithm
API_HASH_SHA256 to use the SHA-256 algorithm
API_HASH_SHA384 to use the SHA-384 algorithm
API_HASH_SHA512 to use the SHA-512 algorithm
API_HASH_MD5 to use the MD5 algorithm
API_HASH_MD2 to use the MD2 algorithm (legacy)
API_HASH_RMD160 to use the RIPEMD-160 algorithm
API_HASH_SHA3_224 to use the SHA-3-224 algorithm
API_HASH_SHA3_256 to use the SHA-3-256 algorithm
API_HASH_SHA3_384 to use the SHA-3-384 algorithm
API_HASH_SHA3_512 to use the SHA-3-512 algorithm
API_HASH_ASCON_HASH to use the ASCON-HASH algorithm
API_HASH_ASCON_HASHA to use the ASCON-HASHA algorithm

Returns (VBA/C)

If successful, the return value is the number of bytes in the hash digest array; otherwise it returns a negative error code.

VBA Wrapper Syntax

Public Function hashBytes(lpMessage() As Byte, nOptions As Long) As Byte()

.NET Equivalent

Hash.BytesFromBytes Method

Python Equivalent

static Hash.data(data, alg=Alg.SHA1)

Remarks

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.

Example (VBA core function)

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

Example (VBA wrapper function)

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))

See Also

HASH_HexFromBytes HASH_HexFromHex

[Contents] [Index]

[PREV: HASH_AddBytes...]   [Contents]   [Index]   
   [NEXT: HASH_DigestLength...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.