Docs / Crypt

Crypt

Base64, AES-256, SHA/MD5 hashing, HMAC, random bytes, and LZ4 compression.

Library#13 of 23 Functions11
crypt.random(size: number) → string
Cryptographically-random bytes via BCryptGenRandom.
lua
local iv = crypt.random(16)   -- 16 random bytes
crypt.generatebytes(size: number) → string
Random bytes returned as base64.
lua
print(crypt.generatebytes(32))
crypt.generatekey() → string
32-byte base64-encoded AES key.
lua
local key = crypt.generatekey()
writefile("key.b64", key)
crypt.hash(data: string, algorithm: string) → string
Hex digest. Algorithms: md5, sha1, sha256, sha384, sha512.
lua
print(crypt.hash("hello", "sha256"))
--> 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
crypt.hmac(data: string, key: string, algorithm: string) → string
HMAC hex digest (RFC 2104). Key may be raw or hex.
lua
print(crypt.hmac("data", "secret", "sha256"))
crypt.encrypt(data: string, key: string, iv: string?, mode: string?) → string, string
AES-256 encrypt. Returns (ciphertext, iv).
lua
local key = crypt.generatekey()
local ct, iv = crypt.encrypt("hello world", key)
crypt.decrypt(data: string, key: string, iv: string, mode: string?) → string
AES-256 decrypt.
lua
print(crypt.decrypt(ct, key, iv))  --> hello world
crypt.base64encode(data: string) → string
Base64 encode.
aliasescrypt.base64_encode, crypt.encode, base64encode (global), base64_encode (global), base64.encode
lua
print(crypt.base64encode("hello"))  --> aGVsbG8=
crypt.base64decode(data: string) → string
Base64 decode.
aliasescrypt.base64_decode, crypt.decode, base64decode (global), base64_decode (global), base64.decode
lua
print(crypt.base64decode("aGVsbG8="))  --> hello
crypt.lz4compress(data: string) → string
LZ4 compress. Also exposed as top-level lz4compress.
lua
local packed = crypt.lz4compress(("a"):rep(1000))
print(#packed)  --> ~20
crypt.lz4decompress(data: string, decompressedSize: number) → string
LZ4 decompress. Also exposed as top-level lz4decompress.
lua
local original = crypt.lz4decompress(packed, 1000)
print(#original)  --> 1000