Moonshark/runner/lua/crypto.lua

141 lines
3.5 KiB
Lua

-- crypto.lua
-- ======================================================================
-- HASHING FUNCTIONS
-- ======================================================================
-- Generate hash digest using various algorithms
-- Algorithms: md5, sha1, sha256, sha512
-- Formats: hex (default), binary
function hash(data, algorithm, format)
if type(data) ~= "string" then
error("hash: data must be a string", 2)
end
algorithm = algorithm or "sha256"
format = format or "hex"
return __crypto_hash(data, algorithm, format)
end
function md5(data, format)
return hash(data, "md5", format)
end
function sha1(data, format)
return hash(data, "sha1", format)
end
function sha256(data, format)
return hash(data, "sha256", format)
end
function sha512(data, format)
return hash(data, "sha512", format)
end
-- ======================================================================
-- HMAC FUNCTIONS
-- ======================================================================
-- Generate HMAC using various algorithms
-- Algorithms: md5, sha1, sha256, sha512
-- Formats: hex (default), binary
function hmac(data, key, algorithm, format)
if type(data) ~= "string" then
error("hmac: data must be a string", 2)
end
if type(key) ~= "string" then
error("hmac: key must be a string", 2)
end
algorithm = algorithm or "sha256"
format = format or "hex"
return __crypto_hmac(data, key, algorithm, format)
end
function hmac_md5(data, key, format)
return hmac(data, key, "md5", format)
end
function hmac_sha1(data, key, format)
return hmac(data, key, "sha1", format)
end
function hmac_sha256(data, key, format)
return hmac(data, key, "sha256", format)
end
function hmac_sha512(data, key, format)
return hmac(data, key, "sha512", format)
end
-- ======================================================================
-- RANDOM FUNCTIONS
-- ======================================================================
-- Generate random bytes
-- Formats: binary (default), hex
function random_bytes(length, secure, format)
if type(length) ~= "number" or length <= 0 then
error("random_bytes: length must be positive", 2)
end
secure = secure ~= false -- Default to secure
format = format or "binary"
return __crypto_random_bytes(length, secure, format)
end
-- Generate random integer in range [min, max]
function random_int(min, max, secure)
if type(min) ~= "number" or type(max) ~= "number" then
error("random_int: min and max must be numbers", 2)
end
if max <= min then
error("random_int: max must be greater than min", 2)
end
secure = secure ~= false -- Default to secure
return __crypto_random_int(min, max, secure)
end
-- Generate random string of specified length
function random_string(length, charset, secure)
if type(length) ~= "number" or length <= 0 then
error("random_string: length must be positive", 2)
end
secure = secure ~= false -- Default to secure
-- Default character set: alphanumeric
charset = charset or "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
if type(charset) ~= "string" or #charset == 0 then
error("random_string: charset must be non-empty", 2)
end
local result = ""
local charset_length = #charset
for i = 1, length do
local index = random_int(1, charset_length, secure)
result = result .. charset:sub(index, index)
end
return result
end
-- ======================================================================
-- UUID FUNCTIONS
-- ======================================================================
-- Generate random UUID (v4)
function uuid()
return __crypto_uuid()
end