code wiki / (root) / signed_cookie.nx

signed_cookie.nx

buildroot/runtime/signed_cookie.nx

5553 B161 linesdepth 6pulls 8 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

signed_cookie.nx -- HMAC-signed tamper-evident cookies. Browser cookies are stored client-side and editable by the user. For session state you typically want the user to HOLD the state but NOT modify it. Signed cookies solve this: cookie = value || \".\" || base64url(HMAC-SHA-256(key, value)) On read: split at the last dot, verify HMAC with secret key, reject if tampered. Same idea as Rails / Django signed cookies + Express cookie-parser's signed mode + the session cookie in tens of thousands of production Python apps. Composes hmac.nx (HMAC-SHA-256) + base64.nx (for URL-safe signature bytes) + ct.nx (constant-time compare). Invariants: SC1 Only the PAYLOAD is signed -- key isn't revealed by cookie value. Tampering changes MAC -> rejected. SC2 Verification uses ct_memcmp to eliminate timing leaks. SC3 Base64url without padding keeps cookie URL-safe + single-valued (compatible with cookie attribute grammar). SC4 No expiry here -- cookie's own Max-Age / Expires attribute handles that. Replay-attack defence requires caller to include a timestamp in the payload and enforce bounds on read.

dependencies 4 imports · 0 importers

syscalls.nx nx_hmac.nx base64.nx ct.nx signed_cookie.nx

imports: syscalls.nxnx_hmac.nxbase64.nxct.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main signed_cookie_sign sc_sign_bytes hmac_sha256 sys_mmap sha256_digest sys_mmap ↻ sha256_init sys_mmap ↻ sha256_k sha256_update sha256_compress_ni_blocks blk_set_byte sha256_compress sha256_final blk_set_byte ↻ sha256_compress ↻ sha256_init ↻ sha256_update ↻ sha256_final ↻ b64_encode b64_enc_char signed_cookie_verify sc_sign_bytes ↻ ct_memcmp ct_eq

structs

none

consts

37const SC_ERR_SHORT: i64 = -1
38const SC_ERR_FORMAT: i64 = -2
39const SC_ERR_MAC: i64 = -3

functions

43func sc_sign_bytes(key: *u8, key_len: i64,
122func main() -> i64