Skip to content

HMAC Methods Reference

📋 Method List

Traditional Methods

MethodDescription
SetKeySet key from string
SetKeyBytesSet key from byte array
ComputeCompute string HMAC (returns Hex)
ComputeBytesCompute byte array HMAC (returns byte array)
ComputeBytesToHexCompute byte array HMAC (returns Hex)

Chainable Methods

MethodDescription
ModeSet HMAC algorithm
SecretSet key from string/Hex/Base64
SecretBytesSet key from byte array
DataStringInput string data
DataBytesInput byte array data
ReturnHexReturn Hex format result
ReturnBase64Return Base64 format result
ReturnBytesReturn 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 String

Parameters

ParameterTypeDescription
DataStringData to sign
KeyStringStringKey string
AlgorithmHMACAlgorithmOptional, algorithm (defaults to class property)
EncodingHMACStringEncodingOptional, 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 cCryptoHMAC

Secret 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 cCryptoHMAC

Key Types:

  • SECRET_KEY_STRING - Plain string (default)
  • SECRET_KEY_HEX - Hex-encoded key
  • SECRET_KEY_BASE64 - Base64-encoded key

SecretBytes Method

Sets the key from byte array.

vb
Public Function SecretBytes(ByRef KeyBytes() As Byte) As cCryptoHMAC

DataString Method

Inputs string data.

vb
Public Function DataString(ByVal Text As String, _
                           Optional ByVal Encoding As HMACStringEncoding = HMAC_ENCODING_UTF8) As cCryptoHMAC

DataBytes Method

Inputs byte array data.

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

ReturnHex Method

Returns HMAC in hexadecimal format.

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

ReturnBase64 Method

Returns HMAC in Base64 format.

vb
Public Function ReturnBase64() As String

ReturnBytes 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 Function

Message 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 Function

JWT 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 Function

Last Updated: 2026-05-17

VB6 and LOGO copyright of Microsoft Corporation