CryptoSys PKI Pro Manual

HMAC_HexFromHex

Creates a keyed-hash based message authentication code (HMAC) in hexadecimal format from data in hexadecimal-encoded strings. The hash algorithm to use is passed in the options parameter.

VBA/VB6 Syntax

Public Declare Function HMAC_HexFromHex Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strMsgHex As String, ByVal strKeyHex As String, ByVal nOptions As Long) As Long

nRet = HMAC_HexFromHex(strOutput, nOutChars, strMsgHex, strKeyHex, nOptions)

C/C++ Syntax

long __stdcall HMAC_HexFromHex(char *szOutput, long nOutChars, const char *szMsgHex, const char *szKeyHex, long nOptions);

Parameters

szOutput
[out] to receive output in hexadecimal format.
nOutChars
[in] specifying the maximum number of characters to be received.
szMsgHex
[in] containing the message data in hexadecimal-encoded format.
szKeyHex
[in] containing the key in hexadecimal-encoded format.
nOptions
[in] Option flags. Select one of:
PKI_HASH_SHA1 (0) to use the SHA-1 algorithm (default)
PKI_HASH_SHA224 to use the SHA-224 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_SHA3_224 to use the SHA-3-224 algorithm
PKI_HASH_SHA3_256 to use the SHA-3-256 algorithm
PKI_HASH_SHA3_384 to use the SHA-3-384 algorithm
PKI_HASH_SHA3_512 to use the SHA-3-512 algorithm
PKI_HASH_MD5 to use the MD5 algorithm

Returns (VBA/C)

If successful, the return value is the number of characters in or required for the output string; otherwise it returns a negative error code.

VBA Wrapper Syntax

Public Function hmacHexFromHex (szMsgHex As String, szKeyHex As String, nOptions As Long) As String

.NET Equivalent

Hmac.HexFromHex Method

C++ (STL) Equivalent

static std::string dipki::Hmac::HexFromHex (const std::string &dataHex, const std::string &keyHex, Alg alg=Alg::Sha1)

Python Equivalent

static Hmac.hex_from_hex(datahex, keyhex, alg=Alg.SHA1)

Remarks

For the "raw" VBA/C function, the user must allocate an output string buffer szOutput of the required length. Specify a zero nOutChars or an empty string for szOutput to find the required length. ANSI C users must add one to this value when allocating memory.

The maximum number of output characters is PKI_MAX_HASH_CHARS (C/C++ users add one). C/C++ users should add one to this value when allocating memory. The final digest will be truncated to the specified length if less than the expected size. MD2 is not available with the HMAC functions.

Note the order of parameters here (data, key) is different from the usual order HMAC(key, text).

Example (VBA core function)

Dim strDigest As String
Dim nRet As Long
Dim strData As String
Dim strKey As String
' Ref: RFC 2202 and RFC 4231
' Test Case 1
' Key =  0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
'        0b0b0b0b                    (20 bytes)
' Data = 4869205468657265            ("Hi There")

' Compute HMAC-SHA-1
strDigest = String(PKI_SHA1_CHARS, " ")
strData = "4869205468657265"
strKey = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
nRet = HMAC_HexFromHex(strDigest, Len(strDigest), strData, strKey, PKI_HASH_SHA1)
Debug.Print strDigest
' Compute HMAC-SHA-256
strDigest = String(PKI_SHA256_CHARS, " ")
nRet = HMAC_HexFromHex(strDigest, Len(strDigest), strData, strKey, PKI_HASH_SHA256)
Debug.Print strDigest

The above example should produce the following output:

b617318655057264e28bc0b6fb378c8ef146be00
b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7

Example (VBA wrapper function)

Dim lpData() As Byte
Dim lpKey() As Byte
Dim lpDigest() As Byte
Dim strDigest As String

' Test case 4 from RFC 2202 and RFC 4231
lpKey = cnvBytesFromHexStr("0102030405060708090a0b0c0d0e0f10111213141516171819")
' data = 0xcd repeated 50 times
lpData = cnvBytesFromHexStr("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" & _
    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd")
' Bytes <-- Bytes
lpDigest = hmacBytes(lpData, lpKey, PKI_HASH_SHA256)
Debug.Print "HMAC-SHA-256=" & cnvHexStrFromBytes(lpDigest)
Debug.Print "CORRECT     =82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"
' Hex <-- Bytes
strDigest = hmacHexFromBytes(lpData, lpKey, PKI_HASH_SHA256)
Debug.Print "HMAC-SHA-256=" & strDigest

' Hex <-- Hex
' Test case 1 from RFC 2202 and RFC 4231
' Data = "Hi There"
strDigest = hmacHexFromHex("4869205468657265", "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", PKI_HASH_SHA256)
Debug.Print "HMAC-SHA-256=" & strDigest
Debug.Print "CORRECT     =b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"

See Also

HMAC_Bytes HMAC_HexFromBytes

[Contents] [Index]

[PREV: HMAC_HexFromBytes...]   [Contents]   [Index]   
   [NEXT: HPKE_DerivePrivateKey...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-01T11:51:59Z.