Table of contents
How a Token Is Built and Verified
This page walks through exactly what happens, cryptographically, between a successful login and a token being accepted on a later request. It ties together token/crypto.rs, token/security.rs, and the build-time constants referenced in SSO Across Multiple Instances.
The building blocks
| Primitive | Used for | Where |
|---|---|---|
| HKDF-SHA256 | Deriving a 256-bit encryption key from config.secret |
derive_key_from_secret() |
| XChaCha20-Poly1305 | Encrypting the token's contents (authenticated encryption) | encrypt() / decrypt() |
| BLAKE3 | Signing/hashing the token's identity fields | generate_token() |
| A per-build keyed substitution cipher | An extra transform layer, only in non-fast mode |
calcul_factorhash() |
Step 1: build the signed hash (generate_token)
At login, ProxyAuth computes a BLAKE3 hash over six values:
username, secret_with_timestamp, build_time, time_expire, build_rand, token_id
secret_with_timestamp— derived fromconfig.secretand the current expiry window (generate_secret()), not the raw secret itself.build_time/build_rand— compile-time constants baked into the binary (see SSO Across Multiple Instances for why these matter for multi-instance setups).token_id— a fresh random identifier for this specific token (this is what/adm/revoketargets, and what/adm/stats/sessionsreports).
These six values aren't hashed in a fixed order — they're concatenated according to a shuffle order that's itself a compile-time constant (shuffled_order), then joined with : and hashed with BLAKE3:
token_hash = blake3(value_a : value_b : value_c : ... ) // order varies per build
This is why two independently-built instances compute different hashes for the same inputs unless their build constants have been synced.
Step 2: the optional extra transform (fast vs non-fast)
If config.json's fast is false (the default, recommended setting), the hash from step 1 goes through one more transform before being used — calcul_factorhash():
- Split the hash string into 10-character chunks.
- For each chunk, run a substitution cipher keyed by another build-time constant,
build_seed2: letters are rotated using a BLAKE3-derived keystream, and any run of digits is XORed withbuild_seed2. - Re-join the transformed chunks with
-. - Hash the result again with BLAKE3.
if fast:
token_hash = generate_token(...) // step 1 only
else:
token_hash = blake3( calcul_factorhash( generate_token(...) ) ) // + extra layer
This adds a second, independently-keyed transformation on top of the first hash — see fast in the config.json Reference for the speed/security trade-off this represents.
Step 3: assemble and encrypt the token
The pieces are joined into a plain internal string:
"<token_hash>|<expiry_ts>|<user_index>|<token_id>"
...which is then encrypted with XChaCha20-Poly1305 (authenticated encryption — tampering is detected, not just hidden):
key = HKDF-SHA256(secret = config.secret, salt = "proxyauth.hkdf.v1", info = "derive_key_from_secret.v1")
nonce = 24 random bytes (OsRng, fresh every time)
ciphertext = XChaCha20-Poly1305.encrypt(key, nonce, plaintext)
wire_format = base64( TAG_V1 || nonce || ciphertext )
HKDF output for a given secret is cached (an LRU of up to 1024 derived keys) so repeated encryptions don't re-run the derivation every time — the cache key is a BLAKE3 hash of the secret itself, not the secret in plaintext.
The resulting base64 string is what's returned in the login response body, and/or set as the session_token cookie.
Step 4: validating a token on a later request
flowchart TD
A["decrypt(token, key)"] --> B["Split into:<br/>token_hash_claimed · expiry_ts · user_index · token_id"]
B --> C["Look up user by user_index<br/>in config.users"]
C --> D["Check expiry_ts against token_expiry_seconds<br/>reject if too old / malformed"]
D --> E["Recompute the expected hash from scratch<br/>generate_token(...) [+ calcul_factorhash + blake3 if fast == false]"]
E --> F["Compare token_hash_claimed vs recomputed hash<br/>CONSTANT-TIME"]
F --> G["Check token_id against the revoked-tokens map"]
G --> H{"Valid?"}
H -->|yes| I(["Accepted"])
H -->|no| J(["Rejected at whichever step failed"])
classDef step fill:#f8fafc,stroke:#64748b,stroke-width:1px,color:#0f172a
classDef decision fill:#fef3c7,stroke:#d97706,stroke-width:1px,color:#78350f
classDef ok fill:#dcfce7,stroke:#16a34a,stroke-width:1px,color:#14532d
classDef ko fill:#fee2e2,stroke:#dc2626,stroke-width:1px,color:#7f1d1d
class A,B,C,D,E,F,G step
class H decision
class I ok
class J ko
Nothing about the token's validity is stored server-side except the revocation list — a token is "self-verifying": if it decrypts, its hash matches a value ProxyAuth can independently recompute, and it isn't on the revoked list, it's accepted. This is why the build constants and secret need to match across instances for tokens to be portable (see SSO Across Multiple Instances), and why revocation needs its own explicit tracking (an otherwise-valid, unexpired token has no other way to be invalidated early).
Why two layers of hashing/encryption instead of one
It's worth being explicit about what each layer is actually for, since it can look redundant at a glance:
- The BLAKE3 hash (step 1, optionally deepened in step 2) is what's actually compared on validation — it's the "is this token's content genuine" check, tied to build-time constants that aren't present anywhere in
config.json. - The XChaCha20-Poly1305 encryption (step 3) is what makes the token opaque and tamper-evident on the wire — without it, the plaintext
hash|expiry|index|idstructure (including the rawuser_index) would be visible to anyone holding the token, and nothing would stop a client from handing you a syntactically-plausible but fabricated string to probe the hash-comparison logic directly.
An attacker needs to defeat both — decrypt without the key, and independently reproduce the correct hash without knowing the build constants — not just one or the other.
See also
- config.json Reference —
secret,fast,token_expiry_seconds. - SSO Across Multiple Instances — why
build_time/build_rand/shuffle order need to match across instances. - Token Revocation & Multi-Instance Sync — the one piece of token state that is tracked server-side.
📖 Wiki Home · 💻 Source Code · 🐛 Report an Issue · 📜 License
Found an error or something outdated on this page? Open an issue or submit a PR against the wiki repository.