Skip to content

Hash Methods Reference

📋 Method List

Traditional Methods

MethodDescription
ComputeHashCompute string hash (returns Hex)
ComputeHashBytesToHexCompute byte array hash (returns Hex)
ComputeHashBytesCompute byte array hash (returns byte array)
ComputeFileHashCompute file hash

Chainable Methods

MethodDescription
ModeSet hash algorithm
DataStringInput string data
DataBytesInput byte array data
ReturnHexReturn Hex format result
ReturnBase64Return Base64 format result
ReturnBytesReturn byte array result

🔧 ComputeHash Method

Description

Computes the hash value of a string, returning a hexadecimal string.

Syntax

vb
Public Function ComputeHash(ByVal Text As String, _
                            Optional ByVal Algorithm As CryptoHashAlgorithm, _
                            Optional ByVal Encoding As CryptoStringEncoding = ENCODING_UTF8) As String

Parameters

ParameterTypeDescription
TextStringText string to hash
AlgorithmCryptoHashAlgorithmOptional, hash algorithm (defaults to class property value)
EncodingCryptoStringEncodingOptional, string encoding (default UTF8)

Example

vb
Dim Hash As New cCryptoHash

' Default SHA256
Dim result As String
result = Hash.ComputeHash("Hello World")

' Specify MD5
result = Hash.ComputeHash("Hello World", HASH_ALG_MD5)

' Specify ANSI encoding
result = Hash.ComputeHash("Hello World", HASH_ALG_SHA256, ENCODING_ANSI)

📦 ComputeHashBytes Method

Description

Computes the hash of a byte array, returning the raw byte array.

Syntax

vb
Public Function ComputeHashBytes(ByRef Data() As Byte, _
                                 Optional ByVal Algorithm As CryptoHashAlgorithm) As Byte()

Example

vb
Dim Hash As New cCryptoHash
Dim data() As Byte
Dim result() As Byte

' Prepare byte array
data = StrConv("Hello World", vbFromUnicode)

' Compute hash
result = Hash.ComputeHashBytes(data)

' Result length
Debug.Print "Hash length: " & UBound(result) + 1  ' SHA256 = 32 bytes

📁 ComputeFileHash Method

Description

Computes the hash of a file (binary mode read), returning a hexadecimal string.

Syntax

vb
Public Function ComputeFileHash(ByVal FilePath As String, _
                                 Optional ByVal Algorithm As CryptoHashAlgorithm) As String

Example

vb
Dim Hash As New cCryptoHash
Dim fileHash As String

' Compute file hash
fileHash = Hash.ComputeFileHash("C:\data.txt")
Debug.Print "File SHA256: " & fileHash

' Specify algorithm
fileHash = Hash.ComputeFileHash("C:\data.txt", HASH_ALG_MD5)

⛓️ Chainable Methods

Mode Method

Sets the hash algorithm, starts chainable call.

vb
Public Function Mode(ByVal Algorithm As CryptoHashAlgorithm) As cCryptoHash

DataString Method

Inputs string data.

vb
Public Function DataString(ByVal Text As String, _
                          Optional ByVal Encoding As CryptoStringEncoding = ENCODING_UTF8) As cCryptoHash

DataBytes Method

Inputs byte array data.

vb
Public Function DataBytes(ByRef Data() As Byte) As cCryptoHash

ReturnHex Method

Returns hash value in hexadecimal format.

vb
Public Function ReturnHex(Optional ByVal UpperCase As Boolean = False) As String

ReturnBase64 Method

Returns hash value in Base64 format.

vb
Public Function ReturnBase64() As String

ReturnBytes Method

Returns hash value as byte array.

vb
Public Function ReturnBytes() As Byte()

📌 Chainable Call Examples

Basic Chainable Call

vb
Dim Hash As New cCryptoHash
Dim result As String

' Complete chainable call
result = Hash.Mode(HASH_ALG_SHA256) _
            .DataString("Hello World") _
            .ReturnHex()

Omit Mode (using default SHA256)

vb
result = Hash.DataString("Hello World").ReturnHex()

Multiple Output Formats

vb
' Set data and algorithm
Hash.Mode(HASH_ALG_SHA256).DataString("Hello World")

' Get different formats (without recomputing)
Debug.Print "Hex: " & Hash.ReturnHex()
Debug.Print "Hex(Upper): " & Hash.ReturnHex(True)
Debug.Print "Base64: " & Hash.ReturnBase64()

Byte Array Input

vb
Dim data() As Byte
data = StrConv("Hello World", vbFromUnicode)

result = Hash.Mode(HASH_ALG_SHA256) _
            .DataBytes(data) _
            .ReturnHex()

📌 Usage Scenarios Summary

vb
Private Function HashPassword(ByVal password As String, ByVal salt As String) As String
    Dim Hash As New cCryptoHash
    
    ' Password + salt
    Dim saltedPassword As String
    saltedPassword = password & salt
    
    ' Use SHA256
    HashPassword = Hash.ComputeHash(saltedPassword, HASH_ALG_SHA256)
End Function

File Integrity Verification

vb
Private Function VerifyFileIntegrity(ByVal filePath As String, ByVal expectedHash As String) As Boolean
    Dim Hash As New cCryptoHash
    Dim actualHash As String
    
    ' Compute file hash
    actualHash = Hash.ComputeFileHash(filePath, HASH_ALG_SHA256)
    
    ' Compare (case-insensitive)
    VerifyFileIntegrity = (LCase(actualHash) = LCase(expectedHash))
End Function

Data Signing Pre-hash

vb
Private Function PrepareDataForSigning(ByVal data As String) As Byte()
    Dim Hash As New cCryptoHash
    
    ' Compute hash and return byte array
    PrepareDataForSigning = Hash.Mode(HASH_ALG_SHA256) _
                                  .DataString(data) _
                                  .ReturnBytes()
End Function

Last Updated: 2026-05-17

VB6 and LOGO copyright of Microsoft Corporation