Skip to content

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.

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
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
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
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
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.

Parameters

data: buffer
variant: number?

Returns

string
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.

Parameters

base64: string
variant: number?

Returns

buffer