crypt.hmac

Generates an HMAC (Hash-based Message Authentication Code).

Syntax

crypt.hmac(key: string, data: string, algorithm: string) -> string

Parameters

ParameterTypeDescription
keystringThe secret key
datastringThe data to authenticate
algorithmstringRequired hash algorithm name

Returns

TypeDescription
stringThe HMAC as a Base64 string

Description

crypt.hmac generates a keyed-hash message authentication code, which provides both data integrity and authentication.

Supported Algorithms

  • MD5
  • SHA1
  • SHA224
  • SHA256
  • SHA384
  • SHA512
  • SHA3-224, SHA3-256, SHA3-384, SHA3-512
  • BLAKE2B

Example

local data = "Important message"
local key = "secret_key"

local mac = crypt.hmac(key, data, "SHA256")
print("HMAC:", mac)

Verification Example

local function verifyMessage(data, key, expectedHmac)
    local computed = crypt.hmac(key, data, "SHA256")
    return computed == expectedHmac
end

local key = "my_secret"
local message = "Hello"
local signature = crypt.hmac(key, message, "SHA256")

-- Later, verify the message
if verifyMessage(message, key, signature) then
    print("Message is authentic!")
end