HMAC Methods Reference
📋 Method List
Traditional Methods
| Method | Description |
|---|---|
SetKey | Set key from string |
SetKeyBytes | Set key from byte array |
Compute | Compute string HMAC (returns Hex) |
ComputeBytes | Compute byte array HMAC (returns byte array) |
ComputeBytesToHex | Compute byte array HMAC (returns Hex) |
Chainable Methods
| Method | Description |
|---|---|
Mode | Set HMAC algorithm |
Secret | Set key from string/Hex/Base64 |
SecretBytes | Set key from byte array |
DataString | Input string data |
DataBytes | Input byte array data |
ReturnHex | Return Hex format result |
ReturnBase64 | Return Base64 format result |
ReturnBytes | Return byte array result |
🔑 SetKey / SetKeyBytes Methods
Description
Sets the HMAC key (traditional way).
Syntax
vb
' Set from string
Public Sub SetKey(ByVal KeyString As String, _
Optional ByVal Encoding As HMACStringEncoding = HMAC_ENCODING_UTF8)
' Set from byte array
Public Sub SetKeyBytes(ByRef KeyBytes() As Byte)Example
vb
Dim Hmac As New cCryptoHMAC
' Set UTF-8 key
Hmac.SetKey "my-secret-key"
' Set ANSI key
Hmac.SetKey "my-secret-key", HMAC_ENCODING_ANSI
' Set byte array key
Dim keyBytes() As Byte
keyBytes = StrConv("my-key", vbFromUnicode)
Hmac.SetKeyBytes keyBytes🔐 Compute Method
Description
Computes HMAC of string data, returning a hexadecimal string.
Syntax
vb
Public Function Compute(ByVal Data As String, _
ByVal KeyString As String, _
Optional ByVal Algorithm As HMACAlgorithm, _
Optional ByVal Encoding As HMACStringEncoding = HMAC_ENCODING_UTF8) As StringParameters
| Parameter | Type | Description |
|---|---|---|
Data | String | Data to sign |
KeyString | String | Key string |
Algorithm | HMACAlgorithm | Optional, algorithm (defaults to class property) |
Encoding | HMACStringEncoding | Optional, encoding (default UTF8) |
Example
vb
Dim Hmac As New cCryptoHMAC
Dim result As String
' Simple computation
result = Hmac.Compute("data to sign", "secret-key")
' Specify algorithm
result = Hmac.Compute("data to sign", "secret-key", HMAC_ALG_SHA256)⛓️ Chainable Methods
Mode Method
Sets the HMAC algorithm.
vb
Public Function Mode(ByVal Algorithm As HMACAlgorithm) As cCryptoHMACSecret Method
Sets the key, supports string, Hex, Base64 formats.
vb
Public Function Secret(ByVal KeyString As String, _
Optional ByVal KeyType As SecretKeyType = SECRET_KEY_STRING, _
Optional ByVal Encoding As HMACStringEncoding = HMAC_ENCODING_UTF8) As cCryptoHMACKey Types:
SECRET_KEY_STRING- Plain string (default)SECRET_KEY_HEX- Hex-encoded keySECRET_KEY_BASE64- Base64-encoded key
SecretBytes Method
Sets the key from byte array.
vb
Public Function SecretBytes(ByRef KeyBytes() As Byte) As cCryptoHMACDataString Method
Inputs string data.
vb
Public Function DataString(ByVal Text As String, _
Optional ByVal Encoding As HMACStringEncoding = HMAC_ENCODING_UTF8) As cCryptoHMACDataBytes Method
Inputs byte array data.
vb
Public Function DataBytes(ByRef Data() As Byte) As cCryptoHMACReturnHex Method
Returns HMAC in hexadecimal format.
vb
Public Function ReturnHex(Optional ByVal UpperCase As Boolean = False) As StringReturnBase64 Method
Returns HMAC in Base64 format.
vb
Public Function ReturnBase64() As StringReturnBytes Method
Returns HMAC as byte array.
vb
Public Function ReturnBytes() As Byte()📌 Chainable Call Examples
Basic Chainable Call
vb
Dim Hmac As New cCryptoHMAC
Dim result As String
' Complete chainable call
result = Hmac.Mode(HMAC_ALG_SHA256) _
.Secret("secret-key") _
.DataString("data to sign") _
.ReturnHex()Using Hex Key
vb
' Use Hex-formatted key
result = Hmac.Mode(HMAC_ALG_SHA256) _
.Secret("deadbeef123456", SECRET_KEY_HEX) _
.DataString("data") _
.ReturnHex()Multiple Output Formats
vb
' Set key and data
Hmac.Mode(HMAC_ALG_SHA256) _
.Secret("secret") _
.DataString("data")
' Get different formats
Debug.Print "Hex: " & Hmac.ReturnHex()
Debug.Print "Hex(Upper): " & Hmac.ReturnHex(True)
Debug.Print "Base64: " & Hmac.ReturnBase64()📌 Usage Scenarios Summary
API Request Signing
vb
Private Function SignApiRequest(ByVal method As String, _
ByVal path As String, _
ByVal timestamp As String, _
ByVal apiSecret As String) As String
Dim Hmac As New cCryptoHMAC
Dim dataToSign As String
' Build signature string
dataToSign = method & "|" & path & "|" & timestamp
' Compute HMAC
SignApiRequest = Hmac.Mode(HMAC_ALG_SHA256) _
.Secret(apiSecret) _
.DataString(dataToSign) _
.ReturnHex()
End FunctionMessage Authentication
vb
Private Function CreateMessageAuth(ByVal message As String, _
ByVal sharedSecret As String) As String
Dim Hmac As New cCryptoHMAC
' Create message authentication code
CreateMessageAuth = Hmac.Compute(message, sharedSecret, HMAC_ALG_SHA256)
End Function
Private Function VerifyMessageAuth(ByVal message As String, _
ByVal mac As String, _
ByVal sharedSecret As String) As Boolean
Dim Hmac As New cCryptoHMAC
Dim expectedMac As String
' Recompute MAC
expectedMac = Hmac.Compute(message, sharedSecret, HMAC_ALG_SHA256)
' Compare (case-insensitive)
VerifyMessageAuth = (LCase(mac) = LCase(expectedMac))
End FunctionJWT Signing
vb
Private Function CreateJwtSignature(ByVal headerBase64 As String, _
ByVal payloadBase64 As String, _
ByVal secret As String) As String
Dim Hmac As New cCryptoHMAC
Dim signingInput As String
' JWT signing input
signingInput = headerBase64 & "." & payloadBase64
' HS256 signature
CreateJwtSignature = Hmac.Mode(HMAC_ALG_SHA256) _
.Secret(secret) _
.DataString(signingInput) _
.ReturnBase64()
End FunctionLast Updated: 2026-05-17