CryptoSys API Library Manual

HASH_AddBytes

Adds an array of bytes to be digested in the HASH context.

VBA/VB6 Syntax

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

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

C/C++ Syntax

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

Parameters

hContext
[in] Handle to the HASH 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 hashAddBytes(hContext As Long, lpData() As Byte) As Long
Public Function hashAddString(hContext As Long, szData As String) As Long

.NET Equivalent

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

C++ (STL) Equivalent

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

Remarks

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

VBA users can use the more convenient wrapper functions hashAddBytes and hashAddString.

Example (VBA core function)

Dim hContext As Long
Dim r As Long
Dim nBytes As Long
Dim lpMessage1() As Byte
Dim lpMessage2() As Byte
Dim lpDigest() As Byte

hContext = HASH_Init(API_HASH_SHA1)
Debug.Print "HASH_Init returns 0x" & Hex(hContext)
' Check for an invalid handle
If hContext = 0 Then
  MsgBox "Failed to set context"
  Exit Sub
End If

' Create two arrays of bytes to digest (long way!)
ReDim lpMessage1(0) ' 1 bytes long
lpMessage1(0) = &H61     ' ASCII 'a'
ReDim lpMessage2(1) ' 2 bytes long
lpMessage2(0) = &H62     ' ASCII 'b'
lpMessage2(1) = &H63     ' ASCII 'c'

' Add data in parts (the string "abc")
r = HASH_AddBytes(hContext, lpMessage1(0), 1)
r = HASH_AddBytes(hContext, lpMessage2(0), 2)
nBytes = HASH_DigestLength(hContext)
Debug.Print "HASH_DigestLength=" & nBytes
ReDim lpDigest(nBytes - 1)
nBytes = HASH_Final(lpDigest(0), nBytes, hContext)
Debug.Print "DIG=" & cnvHexStrFromBytes(lpDigest)
Debug.Print "OK= " & "A9993E364706816ABA3E25717850C26C9CD0D89D"

Example (VBA wrapper function)

Dim hContext As Long
Dim lpDigest() As Byte
' Initialize context to use SHA-3-256 (NB HASH_ func)
hContext = hashInit(API_HASH_SHA3_256)
Debug.Print "hashInit returns 0x" & Hex(hContext)
' Check for an invalid handle
If hContext = 0 Then
  MsgBox "Failed to set context"
  Exit Sub
End If
' Add "abc" in parts
Call hashAddBytes(hContext, cnvBytesFromHexStr("61"))   ' ASCII "a"
Call hashAddString(hContext, "")    ' Empty string => No effect
Call hashAddBytes(hContext, cnvBytesFromHexStr(""))    ' Empty byte array => No effect
Call hashAddString(hContext, "bc")  ' Add a string directly
' Get final digest value
lpDigest = hashFinal(hContext)
Debug.Print "DIG=" & cnvHexStrFromBytes(lpDigest)
Debug.Print "OK= " & "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"

See Also

HASH_Init HASH_Final

[Contents] [Index]

[PREV: GCM_NextEncrypt...]   [Contents]   [Index]   
   [NEXT: HASH_Bytes...]

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