--[[ crypto.lua - Cryptographic functions powered by Go ]]-- local crypto = {} -- ====================================================================== -- HASHING FUNCTIONS -- ====================================================================== -- Generate hash digest using various algorithms -- Algorithms: md5, sha1, sha256, sha512 -- Formats: hex (default), binary function crypto.hash(data, algorithm, format) if type(data) ~= "string" then error("crypto.hash: data must be a string", 2) end algorithm = algorithm or "sha256" format = format or "hex" return __crypto_hash(data, algorithm, format) end -- Convenience functions for common hash algorithms function crypto.md5(data, format) return crypto.hash(data, "md5", format) end function crypto.sha1(data, format) return crypto.hash(data, "sha1", format) end function crypto.sha256(data, format) return crypto.hash(data, "sha256", format) end function crypto.sha512(data, format) return crypto.hash(data, "sha512", format) end -- ====================================================================== -- HMAC FUNCTIONS -- ====================================================================== -- Generate HMAC using various algorithms -- Algorithms: md5, sha1, sha256, sha512 -- Formats: hex (default), binary function crypto.hmac(data, key, algorithm, format) if type(data) ~= "string" then error("crypto.hmac: data must be a string", 2) end if type(key) ~= "string" then error("crypto.hmac: key must be a string", 2) end algorithm = algorithm or "sha256" format = format or "hex" return __crypto_hmac(data, key, algorithm, format) end -- Convenience functions for common HMAC algorithms function crypto.hmac_md5(data, key, format) return crypto.hmac(data, key, "md5", format) end function crypto.hmac_sha1(data, key, format) return crypto.hmac(data, key, "sha1", format) end function crypto.hmac_sha256(data, key, format) return crypto.hmac(data, key, "sha256", format) end function crypto.hmac_sha512(data, key, format) return crypto.hmac(data, key, "sha512", format) end -- ====================================================================== -- RANDOM FUNCTIONS -- ====================================================================== -- Generate random bytes -- Formats: binary (default), hex function crypto.random_bytes(length, secure, format) if type(length) ~= "number" or length <= 0 then error("crypto.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 crypto.random_int(min, max, secure) if type(min) ~= "number" or type(max) ~= "number" then error("crypto.random_int: min and max must be numbers", 2) end if max <= min then error("crypto.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 crypto.random_string(length, charset, secure) if type(length) ~= "number" or length <= 0 then error("crypto.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("crypto.random_string: charset must be non-empty", 2) end local result = "" local charset_length = #charset for i = 1, length do local index = crypto.random_int(1, charset_length, secure) result = result .. charset:sub(index, index) end return result end -- ====================================================================== -- UUID FUNCTIONS -- ====================================================================== -- Generate random UUID (v4) function crypto.uuid() return __crypto_uuid() end return crypto