CryptoSys API Library Manual

MAC_AddBytes

Adds an array of bytes to be authenticated in the MAC context.

VBA/VB6 Syntax

Public Declare Function MAC_AddBytes Lib "diCryptoSys.dll" (ByVal hContext As Long, ByRef lpData As Byte, ByVal nDataLen As Long) As Long

nRet = MAC_AddBytes(hContext, abData(0), nDataLen)

C/C++ Syntax

long __stdcall MAC_AddBytes(long hContext, const void *lpData, long nDataLen);

Parameters

hContext
[in] Handle to the MAC context.
lpData
[in] Byte array containing the next part of the message.
nDataLen
[in] Number of bytes in the array

Returns (VBA/C)

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

VBA Wrapper Syntax

Public Function macAddBytes(hContext As Long, lpData() As Byte) As Long
Public Function macAddString(hContext As Long, szData As String) As Long

.NET Equivalent

Mac.AddData Method (Byte[])
Mac.AddData Method (String)

C++ (STL) Equivalent

int crsysapi::Mac::AddData (bvec_t data)
int crsysapi::Mac::AddData (std::string s)

Remarks

The handle hContext to the context must have been set up with a prior call to MAC_Init. The MAC_AddBytes function may be called many times before creating the final message digest with MAC_Final.

VBA users can use the more convenient wrapper functions macAddBytes and macAddString.

Example (VBA core function)

Example (VBA wrapper function)

' Test case 4 from RFC 2202 and RFC 4231
' key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
' key_len         25
' data =          0xcd repeated 50 times
' data_len =      50
Dim hContext As Long
Dim r As Long
Dim lpKey() As Byte
Dim lpMsg10() As Byte
Dim lpDigest() As Byte
Dim i As Long
lpKey = cnvBytesFromHexStr("0102030405060708090a0b0c0d0e0f10111213141516171819")
lpMsg10 = cnvBytesFromHexStr("cdcdcdcdcdcdcdcdcdcd")    ' 0xcd repeated 10 times

' HMAC-SHA-1
hContext = macInit(lpKey, API_HMAC_SHA1)
Debug.Print "macInit returns 0x" & Hex(hContext) & " (expected nonzero)"
For i = 1 To 5
    r = macAddBytes(hContext, lpMsg10)
    Debug.Print "macAddBytes returns " & r
Next
lpDigest = macFinal(hContext)
Debug.Print "MAC=" & cnvHexStrFromBytes(lpDigest)
Debug.Print "OK= " & "4c9007f4026250c6bc8414f9bf50c86c2d7235da"

' HMAC-SHA-256
hContext = macInit(lpKey, API_HMAC_SHA256)
Debug.Print "macInit returns 0x" & Hex(hContext) & " (expected nonzero)"
For i = 1 To 5
    r = macAddBytes(hContext, lpMsg10)
    'Debug.Print "macAddBytes returns " & r
Next
lpDigest = macFinal(hContext)
Debug.Print "MAC=" & cnvHexStrFromBytes(lpDigest)
Debug.Print "OK= " & "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"

Dim strData As String
Dim strKey As String
' Test case 2 from RFC 2202 and RFC 4231
strData = "what do ya want for nothing?"
strKey = "Jefe"
lpKey = StrConv(strKey, vbFromUnicode)
hContext = macInit(lpKey, API_HMAC_SHA512)
' Split input into parts
r = macAddString(hContext, "what do ya")
r = macAddString(hContext, " want for ")
r = macAddString(hContext, "nothing?")
lpDigest = macFinal(hContext)
Debug.Print "MAC=" & cnvHexStrFromBytes(lpDigest)
Debug.Print "OK =" _
& "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" _
& "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737" 

See Also

MAC_Init MAC_Final

[Contents] [Index]

[PREV: HASH_Reset...]   [Contents]   [Index]   
   [NEXT: MAC_Bytes...]

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