Hash Methods Reference
📋 Method List
Traditional Methods
| Method | Description |
|---|---|
ComputeHash | Compute string hash (returns Hex) |
ComputeHashBytesToHex | Compute byte array hash (returns Hex) |
ComputeHashBytes | Compute byte array hash (returns byte array) |
ComputeFileHash | Compute file hash |
Chainable Methods
| Method | Description |
|---|---|
Mode | Set hash algorithm |
DataString | Input string data |
DataBytes | Input byte array data |
ReturnHex | Return Hex format result |
ReturnBase64 | Return Base64 format result |
ReturnBytes | Return 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 StringParameters
| Parameter | Type | Description |
|---|---|---|
Text | String | Text string to hash |
Algorithm | CryptoHashAlgorithm | Optional, hash algorithm (defaults to class property value) |
Encoding | CryptoStringEncoding | Optional, 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 StringExample
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 cCryptoHashDataString Method
Inputs string data.
vb
Public Function DataString(ByVal Text As String, _
Optional ByVal Encoding As CryptoStringEncoding = ENCODING_UTF8) As cCryptoHashDataBytes Method
Inputs byte array data.
vb
Public Function DataBytes(ByRef Data() As Byte) As cCryptoHashReturnHex Method
Returns hash value in hexadecimal format.
vb
Public Function ReturnHex(Optional ByVal UpperCase As Boolean = False) As StringReturnBase64 Method
Returns hash value in Base64 format.
vb
Public Function ReturnBase64() As StringReturnBytes 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
Password Storage (SHA256 Recommended)
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 FunctionFile 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 FunctionData 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 FunctionLast Updated: 2026-05-17