Cryptography

crypt.hmac

Creates a keyed HMAC digest for authenticating a message.

Syntax

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

Arguments

NameTypeDescription
keystringKey used to authenticate the message.
datastringMessage or byte string to authenticate.
algorithm?"sha256" | "sha1" | "sha224" | "sha384" | "sha512" | "md5"HMAC algorithm. Defaults to sha256.

Returns

NameTypeDescription
digeststringBase64-encoded HMAC digest.

Description

Creates a keyed HMAC digest for authenticating a message.

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

Authenticate a message with a caller-provided key.

local message = "payload"
local key = crypt.generatekey()
local digest = crypt.hmac(key, message, "sha256")

print(digest)