crypt.hmac
Generates an HMAC (Hash-based Message Authentication Code).
Syntax
crypt.hmac(data: string, key: string, algorithm: string?) -> stringParameters
| Parameter | Type | Description |
|---|---|---|
data | string | The data to authenticate |
key | string | The secret key |
algorithm | string | (Optional) Hash algorithm, default "SHA256" |
Returns
| Type | Description |
|---|---|
string | The HMAC as a hex string |
Description
crypt.hmac generates a keyed-hash message authentication code, which provides both data integrity and authentication.
Supported Algorithms
MD5SHA1SHA256(default)SHA384SHA512
Example
local data = "Important message"
local key = "secret_key"
local mac = crypt.hmac(data, key, "SHA256")
print("HMAC:", mac)Verification Example
local function verifyMessage(data, key, expectedHmac)
local computed = crypt.hmac(data, key)
return computed == expectedHmac
end
local key = "my_secret"
local message = "Hello"
local signature = crypt.hmac(message, key)
-- Later, verify the message
if verifyMessage(message, key, signature) then
print("Message is authentic!")
endRelated Functions
crypt.hash- Simple hashing