CryptoSys API Library Manual

SHA2_BytesHash

Creates a SHA-256 message digest in Byte array format from a message in Byte array format.

VB6/VBA Syntax

Public Declare Function SHA2_BytesHash Lib "diCryptoSys.dll" (ByRef abDigest As Byte, ByRef abData As Byte, ByVal nDataLen As Long) As Long

nRet = SHA2_BytesHash(abDigest(0), abData(0), nDataLen)

Parameters

abDigest
[out] Byte array to receive message digest.
abData
[in] Byte array containing the message to be hashed.
nDataLen
[in] Long containing number of bytes in the array.

C/C++ Syntax

long _stdcall SHA2_BytesHash(unsigned char *digest, const unsigned char *bytes, long len);

Returns (VB6/C)

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

.NET Equivalent

Sha256.BytesHash Method

Remarks

This function differs from other SHA2 functions in that it creates a digest in Byte array format. This is particularly useful when you need to carry out repeated hash operations. abDigest must be at least 32 (API_MAX_SHA2_BYTES) bytes long.

VB6/VBA users: Note the '(0)' in abDigest(0) and abData(0).

Examples

    Dim nRet As Long
    Dim abData(2) As Byte   ' Create 3-byte array (NB zero-based)
    Dim abDigest(31) As Byte ' Create 32-byte array to receive digest
    
    ' Setup byte array with "abc"
    abData(0) = Asc("a")
    abData(1) = Asc("b")
    abData(2) = Asc("c")
    
    ' Compute SHA-256 hash digest
    nRet = SHA2_BytesHash(abDigest(0), abData(0), 3)
    
    ' Print resulting bytes in hex format
    Debug.Print nRet; cnvHexStrFromBytes(abDigest)

This should result in output as follows:

 0 BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

This second example shows how to carry out repeated hashes of the digest.

    Dim nRet As Long
    Dim abData(2) As Byte   ' Create 3-byte array (NB zero-based)
    Dim abDigest(31) As Byte ' Create 32-byte array to receive digest
    Dim i As Integer
    
    ' Setup byte array with "abc"
    abData(0) = Asc("a")
    abData(1) = Asc("b")
    abData(2) = Asc("c")
    
    ' Compute SHA-256 hash digest of input
    nRet = SHA2_BytesHash(abDigest(0), abData(0), 3)
    
    ' Now carry out repeated hashes of the digest
    For i = 2 To 1000
        nRet = SHA2_BytesHash(abDigest(0), abDigest(0), 32)
    Next
   
    ' Print H(1000) in hex format
    Debug.Print cnvHexStrFromBytes(abDigest)

This should result in output as follows:

FC8A6B86A13F71CD9A67F558AB6FD82A3DD89186163A017ED8051ACF6D3F8F99

See Also

SHA1_BytesHash PBE_Kdf2

[Contents] [Index]

[HOME]   [NEXT: SHA2_BytesHexHash...]

Copyright © 2001-11 D.I. Management Services Pty Ltd. All rights reserved.