CryptoSys API Library Manual

MD5_Hmac

Is a keyed-hash function that provides message authentication using the HMAC algorithm and the MD5 hash function.

VBA/VB6 Syntax

Public Declare Function MD5_Hmac Lib "diCryptoSys.dll" (ByVal strDigest As String, ByRef lpData As Byte, ByVal nDataLen As Long, ByRef lpKey As Byte, ByVal nKeyLen As Long) As Long

nRet = MD5_Hmac(strDigest, abData(0), nDataLen, abKey(0), nKeyLen) ' Note the "(0)" after the byte array parameters

C/C++ Syntax

long __stdcall MD5_Hmac(char *szDigest, const unsigned char *textBytes, long textLen, const unsigned char *lpKeyBytes, long keyLen);

Parameters

szDigest
[out] variable of sufficient length to receive the message digest in hex format.
textBytes
[in] array containing the text of the message.
textLen
[in] containing the number of bytes in the array
lpKeyBytes
[in] array containing the key.
keyLen
[in] containing the number of bytes in the key

Returns (VBA/C)

If successful, the return value is 0; otherwise it returns a non-zero error code.

.NET Equivalent

Md5.Hmac Method (Byte[], Byte[])

Remarks

szDigest must be at least 32 (API_MAX_MD5_CHARS) characters long (33 in a C program).

Example

This example reproduces the three test vectors from RFC 2104.

    Dim nRet As Long
    Dim abData() As Byte
    Dim abKey() As Byte
    Dim i As Integer
    Dim nDataLen As Long, nKeyLen As Long
    Dim strDigest As String * 32

    ' Test No 1.
    ' Set key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
    ReDim abKey(15)
    For i = 0 To 15
        abKey(i) = &HB
    Next
    ' Convert string to byte array
    abData() = StrConv("Hi There", vbFromUnicode)
    nDataLen = UBound(abData) + 1
    ' Create HMAC digest
    nRet = MD5_Hmac(strDigest, abData(0), nDataLen, abKey(0), 16)
    Debug.Print 1; nRet; strDigest

    ' Test No 2.
    abKey() = StrConv("Jefe", vbFromUnicode)
    nKeyLen = UBound(abKey) + 1
    abData() = StrConv("what do ya want for nothing?", vbFromUnicode)
    nDataLen = UBound(abData) + 1
    nRet = MD5_Hmac(strDigest, abData(0), nDataLen, abKey(0), nKeyLen)
    Debug.Print 2; nRet; strDigest

    ' Test No 3.
    ReDim abKey(15)
    For i = 0 To 15
        abKey(i) = &HAA
    Next
    ReDim abData(49)
    For i = 0 To 49
        abData(i) = &HDD
    Next
    nRet = MD5_Hmac(strDigest, abData(0), 50, abKey(0), 16)
    Debug.Print 3; nRet; strDigest

This should result in output as follows:

 1  0 9294727a3638bb1c13f48ef8158bfc9d
 2  0 750c783e6ab0b503eaa86e310a5db738
 3  0 56be34521d144c88dbb8c733f0e8b3f6

See Also

-

[Contents] [Index]

[PREV: MD5_HexDigest...]   [Contents]   [Index]   
   [NEXT: MD5_HmacHex...]

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