Utils
Utility helpers: constant-time comparison, memory zeroing, and hex/base64 encode/decode. All encoding and decoding goes through libsodium, ensuring compatibility with other libsodium consumers.
Summary
Functions
Section titled “Functions”memcmp
Section titled “memcmp”Utils . memcmp ( a , b ) -> boolean
Compares a and b in constant time; returns true if they are equal.
Both buffers must be the same length - use this for all secret-value comparisons to prevent timing attacks.
Parameters
| a: buffer | |
| b: buffer |
Returns
| boolean |
memzero
Section titled “memzero”Utils . memzero ( pointer , length ) -> ()
Zeroes length bytes at pointer in a way the compiler cannot optimise away.
Operates on FFI-allocated memory only; for Luau buffers use buffer.fill instead.
Parameters
| pointer: FFIPointer | |
| length: number |
bin2hex
Section titled “bin2hex”Utils . bin2hex ( data ) -> string
Encodes data as a lowercase hex string via libsodium.
Output length is always 2 * buffer.len(data) characters.
Parameters
| data: buffer |
Returns
| string |
hex2bin
Section titled “hex2bin”Utils . hex2bin ( hex ) -> buffer
Decodes a hex string into a binary buffer; accepts both upper and lower case.
Errors if hex contains characters outside [0-9a-fA-F].
Parameters
| hex: string |
Returns
| buffer |
bin2base64
Section titled “bin2base64”Utils . bin2base64 ( data , variant ) -> string
Encodes data as a base64 string; variant selects the alphabet and padding.
Variants: Sys.base64.VARIANT_ORIGINAL (default), Sys.base64.VARIANT_ORIGINAL_NO_PADDING, BASE64_URLSAFE, BASE64_URLSAFE_NO_PADDING.
base642bin
Section titled “base642bin”Utils . base642bin ( base64 , variant ) -> buffer
Decodes a base64 string into a binary buffer; variant must match the one used during encoding.
Whitespace is not ignored - strip it beforehand. Errors on invalid input.