CryptoSys PKI Toolkit Manual

HMAC_Bytes

Creates a keyed-hash based message authentication code (HMAC) as a byte array from byte data. The hash algorithm to use is passed in the options parameter.

VB6/VBA Syntax

Public Declare Function HMAC_Bytes Lib "diCrPKI.dll" (ByRef abDigest As Byte, ByVal nDigLen As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByRef abKey As Byte, ByVal nKeyLen As Long, ByVal nOptions As Long) As Long

nRet = HMAC_Bytes(abDigest(0), nDigLen, abMessage(0), nMsgLen, abKey(0), nKeyLen, nOptions)

Parameters

abDigest
[out] Byte array to receive the hash digest.
nDigLen
[in] Long specifying the length in bytes of the output array.
abMessage
[in] Byte array containing the message data
nMsgLen
[in] Long specifying length of the message data in bytes.
abKey
[in] Byte array containing the key
nKeyLen
[in] Long specifying length of the key in bytes.
nOptions
[in] Long Option flags. Select one of:
PKI_HASH_SHA1 (0) to use the SHA-1 algorithm (default)
PKI_HASH_MD5 to use the MD5 algorithm
PKI_HASH_SHA256 to use the SHA-256 algorithm
PKI_HASH_SHA384 to use the SHA-384 algorithm
PKI_HASH_SHA512 to use the SHA-512 algorithm
PKI_HASH_SHA224 to use the SHA-224 algorithm

C/C++ Syntax

long _stdcall HMAC_Bytes(unsigned char *lpDigest, long nDigLen, const void *lpMessage, long nMsgLen, const void *lpKey, long nKeyLen, long nOptions);

Returns (VB6/C)

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

.NET Equivalent

Hmac.BytesFromBytes Method

Remarks

Specify a zero nDigLen parameter to find out the required length of the output array, or use the appropriate PKI_XXX_BYTES constant. The maximum possible length is PKI_MAX_HASH_BYTES. The final digest will be truncated to the specified length if less than the expected size. MD2 is not available with the HMAC functions.

VB6/VBA users: Note the '(0)' after the byte array parameters.

Example

Dim abData() As Byte
Dim abKey() As Byte
Dim nDataLen As Long
Dim nKeyLen As Long
Dim abDigest() As Byte
Dim nDigLen As Long
Dim nRet As Long
Dim i As Long

' Test case 4 from RFC 2202 and RFC 4231
' key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
' key_len         25
' data =          0xcd repeated 50 times
' data_len =      50

nKeyLen = 25
ReDim abKey(nKeyLen - 1)
For i = 0 To nKeyLen - 1
    abKey(i) = CByte(i + 1)
Next
Debug.Print "Key=" & cnvHexStrFromBytes(abKey)
nDataLen = 50
ReDim abData(nDataLen - 1)
For i = 0 To nDataLen - 1
    abData(i) = &HCD
Next

' Compute default HMAC (HMAC-SHA-1)
nDigLen = PKI_SHA1_BYTES
ReDim abDigest(nDigLen - 1)
nRet = HMAC_Bytes(abDigest(0), nDigLen, abData(0), nDataLen, abKey(0), nKeyLen, PKI_HASH_SHA1)
If nRet <= 0 Then Exit Sub ' ERROR
Debug.Print "HMAC-SHA-1  =" & cnvHexStrFromBytes(abDigest)
Debug.Print "CORRECT     =" & "4c9007f4026250c6bc8414f9bf50c86c2d7235da"

' Compute HMAC-MD5
nDigLen = PKI_MD5_BYTES
ReDim abDigest(nDigLen - 1)
nRet = HMAC_Bytes(abDigest(0), nDigLen, abData(0), nDataLen, abKey(0), nKeyLen, PKI_HASH_MD5)
If nRet <= 0 Then Exit Sub ' ERROR
Debug.Print "HMAC-MD5    =" & cnvHexStrFromBytes(abDigest)
Debug.Print "CORRECT     =" & "697eaf0aca3a3aea3a75164746ffaa79"

' Compute HMAC-SHA-256
nDigLen = PKI_SHA256_BYTES
ReDim abDigest(nDigLen - 1)
nRet = HMAC_Bytes(abDigest(0), nDigLen, abData(0), nDataLen, abKey(0), nKeyLen, PKI_HASH_SHA256)
If nRet <= 0 Then Exit Sub ' ERROR
Debug.Print "HMAC-SHA-256=" & cnvHexStrFromBytes(abDigest)
Debug.Print "CORRECT     =" & "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"

The above example should produce the following output:

Key=0102030405060708090A0B0C0D0E0F10111213141516171819
HMAC-SHA-1  =4C9007F4026250C6BC8414F9BF50C86C2D7235DA
CORRECT     =4c9007f4026250c6bc8414f9bf50c86c2d7235da
HMAC-MD5    =697EAF0ACA3A3AEA3A75164746FFAA79
CORRECT     =697eaf0aca3a3aea3a75164746ffaa79
HMAC-SHA-256=82558A389A443C0EA4CC819899F2083A85F0FAA3E578F8077A2E3FF46729665B
CORRECT     =82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b

See Also

HMAC_HexFromBytes HMAC_HexFromHex

[Contents] [Index]

[HOME]   [NEXT: HMAC_HexFromBytes...]

Copyright © 2004-12 D.I. Management Services Pty Ltd. All rights reserved.