Hash Functions
Hash Functions
Section titled “Hash Functions”Cryptographic hash functions and HMAC message authentication.
Loading
Section titled “Loading”local hash = require("hash")Cryptographic Hashes
Section titled “Cryptographic Hashes”local hex = hash.md5("data")local raw = hash.md5("data", true)| Parameter | Type | Description |
|---|---|---|
data | string | Data to hash |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
local hex = hash.sha1("data")local raw = hash.sha1("data", true)| Parameter | Type | Description |
|---|---|---|
data | string | Data to hash |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
SHA-256
Section titled “SHA-256”local hex = hash.sha256("data")local raw = hash.sha256("data", true)| Parameter | Type | Description |
|---|---|---|
data | string | Data to hash |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
SHA-512
Section titled “SHA-512”local hex = hash.sha512("data")local raw = hash.sha512("data", true)| Parameter | Type | Description |
|---|---|---|
data | string | Data to hash |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
HMAC Authentication
Section titled “HMAC Authentication”HMAC-MD5
Section titled “HMAC-MD5”local hex = hash.hmac_md5("message", "secret")local raw = hash.hmac_md5("message", "secret", true)| Parameter | Type | Description |
|---|---|---|
data | string | Message to authenticate |
secret | string | Secret key |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
HMAC-SHA1
Section titled “HMAC-SHA1”local hex = hash.hmac_sha1("message", "secret")local raw = hash.hmac_sha1("message", "secret", true)| Parameter | Type | Description |
|---|---|---|
data | string | Message to authenticate |
secret | string | Secret key |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
HMAC-SHA256
Section titled “HMAC-SHA256”local hex = hash.hmac_sha256("message", "secret")local raw = hash.hmac_sha256("message", "secret", true)| Parameter | Type | Description |
|---|---|---|
data | string | Message to authenticate |
secret | string | Secret key |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
HMAC-SHA512
Section titled “HMAC-SHA512”local hex = hash.hmac_sha512("message", "secret")local raw = hash.hmac_sha512("message", "secret", true)| Parameter | Type | Description |
|---|---|---|
data | string | Message to authenticate |
secret | string | Secret key |
raw | boolean? | Return raw bytes instead of hex |
Returns: string, error
Non-Cryptographic Hashes
Section titled “Non-Cryptographic Hashes”FNV-32
Section titled “FNV-32”Fast hash for hash tables and partitioning.
local n = hash.fnv32("data")| Parameter | Type | Description |
|---|---|---|
data | string | Data to hash |
Returns: number, error
FNV-64
Section titled “FNV-64”Fast hash with larger output for reduced collisions.
local n = hash.fnv64("data")| Parameter | Type | Description |
|---|---|---|
data | string | Data to hash |
Returns: number, error
Errors
Section titled “Errors”| Condition | Kind | Retryable |
|---|---|---|
| Input not a string | errors.INVALID | no |
| Secret not a string (HMAC) | errors.INVALID | no |
See Error Handling for working with errors.