API Reference › Crypto
namespace

Crypto

Secure, test-vector-backed hashing for the handful of security-sensitive things a game actually does: signing a save or leaderboard payload so casual tampering is detectable, and verifying that a network message or token was not forged by someone who does not hold the key. This is the deliberate counterpart to the fast Hash library — same idea, opposite trade-off. Hash is fast and reversible and must never guard anything; Crypto is SHA-256 and HMAC-SHA256 implemented to the standard, so the algorithms are the ones with published known-answer tests rather than anything home-grown. Digests are returned as lowercase hex strings, not raw bytes — a str is null-terminated and a raw digest can contain a zero byte, so hex is the form you can print, store, and compare directly. Alongside hashing, this library exposes the OS cryptographically-secure random generator — random_bytes, random_hex, and random_u32 — for tokens, nonces, and Uuid generation, plus base64 for moving bytes through text-only channels. The secure-random helpers are deliberately non-deterministic and must never seed the lockstep simulation RNG (Random). What this is not: it is not DRM and it is not unbeatable anti-cheat. A client-side game cannot keep a secret from the machine it runs on — a determined owner can always read the key out of the binary. Use it to make *casual* tampering detectable and to authenticate messages between parties who share a key. To verify a MAC always use Crypto.verify_hmac (a constant-time check), never ==, which leaks how much of a guessed MAC was correct.

Crypto.sha256(s) -> stringSHA-256 of a string, as 64 hex characters.Crypto.hmac_sha256(key, msg) -> stringSign a message with a shared secret key.Crypto.verify_hmac(key, msg, mac) -> boolConstant-time check that a MAC matches.Crypto.hex(s) -> stringLowercase hex of a string's bytes.Crypto.ct_equal(a, b) -> boolConstant-time string equality for secrets.Crypto.random_bytes(n) -> stringn bytes from the OS CSPRNG, as a 2n-character hex string.Crypto.random_hex(n) -> stringAlias for random_bytes — n secure bytes as a 2n-char hex string.Crypto.random_u32() -> intOne CSPRNG-drawn 32-bit integer.Crypto.base64(s) -> stringStandard base64 (RFC 4648) of a string's bytes.
← All symbols