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.
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)
long __stdcall HMAC_HexFromHex(char *szOutput, long nOutChars, const char *szMsgHex, const char *szKeyHex, long nOptions);
If successful, the return value is the number of characters in or required for the output string; otherwise it returns a negative error code.
Public Function hmacHexFromHex
(szMsgHex As String, szKeyHex As String, nOptions As Long) As String
static std::string dipki::Hmac::HexFromHex (const std::string &dataHex, const std::string &keyHex, Alg alg=Alg::Sha1)
static Hmac.hex_from_hex(datahex, keyhex, alg=Alg.SHA1)
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).
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
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"