Cryptography

crypt.hmac

Computes a crypto library HMAC over Lua string bytes using a raw Lua string key and returns a Base64 digest.

Syntax

crypt.hmac(key: string, data: string, algorithm?: "sha256" | "sha1" | "sha224" | "sha384" | "sha512" | "md5"): string

Arguments

NameTypeDescription
keystringRaw Lua string bytes used as the HMAC key. The key is not Base64-decoded.
datastringLua string bytes passed into the HMAC calculation.
algorithm?"sha256" | "sha1" | "sha224" | "sha384" | "sha512" | "md5"Case-sensitive HMAC algorithm name. Defaults to sha256.

Returns

NameTypeDescription
digeststringBase64-encoded HMAC output with no line breaks.

Description

Computes a crypto library HMAC over Lua string bytes using a raw Lua string key and returns a Base64 digest.

Call it with 3 parameter(s): key, data, algorithm. The argument table explains which values are required and which ones only refine the behavior.

It returns digest (string). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Compute a Base64 HMAC output from a raw Lua string key and data.

local key = "shared secret"
local data = "payload"
local digest = crypt.hmac(key, data, "sha256")

print(digest)