CryptoSys API Library Manual

SHA1_Hmac

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

VBA/VB6 Syntax

Public Declare Function SHA1_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 = SHA1_Hmac(strDigest, abData(0), nDataLen, abKey(0), nKeyLen) ' Note the "(0)" after the byte array parameters

C/C++ Syntax

long __stdcall SHA1_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

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

Remarks

szDigest must be at least 40 (API_MAX_SHA1_CHARS) characters long (41 in a C program).

Example

This example reproduces the three test vectors from RFC 2014 using SHA-1 instead of MD5. Note that the results will be different from those given in RFC 2104 because a different hash function is used (RFC 2104 uses MD5).

    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 * 40

    ' 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 = SHA1_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 = SHA1_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 = SHA1_Hmac(strDigest, abData(0), 50, abKey(0), 16)
    Debug.Print 3; nRet; strDigest

This should result in output as follows:

 1  0 675b0b3a1b4ddf4e124872da6c2f632bfed957e9
 2  0 effcdf6ae5eb2fa2d27416d5f184df9c259a7c79
 3  0 d730594d167e35d5956fd8003d0db3d3f46dc7bb

See Also

SHA1_HmacHex

[Contents] [Index]

[PREV: SHA1_HexDigest...]   [Contents]   [Index]   
   [NEXT: SHA1_HmacHex...]

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