
reverse_resonance_id
Self-checking symmetric tokens based on reversing n² and pairing it with fast integrity tags. The crate ships a zero-allocation core API, an optional CLI, and Criterion benches so you can validate throughput before rollout.
Highlights
- Three schemes: baseline (CRC32 + Blake2s), HMAC (Blake2s) and salt+iter (slow hash w/ configurable work factor).
- Safe, pure-Rust implementation (Rust 1.70+ MSRV) with no
unsafe. - Strong test coverage including property and tamper tests, plus a 100k-token smoke test (opt-in).
- Optional features for CLI (
cli), salted hashing (salt-iter), HMAC (hmac), serde integration, and in-memory key zeroisation (zeroize).
Installation
[]
= { = "0.1.0", = ["hmac", "salt-iter"] }
Feature flags:
| Feature | Default | Purpose |
|---|---|---|
baseline |
✅ | Core CRC32 + Blake2s flow. |
hmac |
✅ | Enables HMAC-Blake2s generation/verification. |
salt-iter |
✅ | Enables salt+iter flow with secure randomness. |
cli |
⛔ | Builds the rrid binary (cargo install --features cli). |
serde |
⛔ | Derives serde::Serialize/Deserialize for structs that need it (used by CLI). |
zeroize |
⛔ | Zeroizes stored secrets (Scheme enum, HMAC helper) when dropped. |
Quick Start (Rust)
use ;
use ;
use ;
Scheme formats:
| Scheme | Format |
|---|---|
| Baseline | <n2>-<rev>-<crc32hex8>-<blake2s8hex> |
| HMAC | <n2>-<rev>-<hmac_blake2s_hex> |
| Salt+Iter | <n2>-<rev>-<salt_hex>-<iters>-<tag8hex> |
Where n2 = n * n as decimal string, rev is that string reversed, and the payload hashed is "{n2}|{rev}".
CLI
Enable the cli feature to build the rrid binary:
Generate
}
}
}
Verify
}
The command exits with code 0 when validation succeeds and 1 otherwise. JSON always contains token, scheme, and the measured runtime in milliseconds.
Errors
Token generation returns Result<Token, RRIDError>. Validation helpers return bool (false on any error). RRIDError variants cover invalid user IDs, format/parse problems, reversed-string mismatches, tag mismatches, and cryptographic errors. Convert or display them directly for logging/auditing.
Security Notes
- Baseline: Fast and deterministic, but only a 32-bit CRC + 32-bit Blake2s tag. Best for human-readable, low-risk use-cases.
- HMAC: Requires a server-side secret. Default tag length is 16 hex chars (64 bits). Increase
tag_lenfor harder brute force. Enable thezeroizefeature to erase in-memory keys on drop. - Salt+Iter: Keyless, time-hardening defence against brute force. Defaults to 2048 iterations; adjust upward for higher security (but note the slower throughput and DoS trade-offs).
- All hex digests are lowercase. Always compare using the provided helpers to avoid subtle mistakes.
- For long-term, high-assurance deployments, consider layering stronger authentication (e.g., Ed25519 signatures) on top—this crate documents the core rev(n²) idea.
Benchmarks
Measured with cargo bench on Rust 1.88.0 (release build) on the developer machine. Throughput counts complete generate+validate pairs.
| Scheme | Workload | Throughput |
|---|---|---|
| Baseline | 100k tokens / batch | ~595k tokens/sec |
| HMAC (tag 16) | 50k tokens / batch | ~506k tokens/sec |
| Salt+Iter (512 iters) | 5k tokens / batch | ~7.9k tokens/sec |
| Salt+Iter (1024 iters) | 5k tokens / batch | ~3.9k tokens/sec |
| Salt+Iter (2048 iters) | 5k tokens / batch | ~2.0k tokens/sec |
| Salt+Iter (4096 iters) | 5k tokens / batch | ~0.93k tokens/sec |
Use the benches to evaluate your own hardware (cargo bench). Plots are stored under target/criterion/.
Testing & QA
The test suite includes:
- Round-trip, tamper, and uniqueness checks for every scheme.
- Property tests with Proptest.
- Deterministic RNG tests for salt generation.
- A long-running
#[ignore]bulk test covering 100k tokens and salt iter smoke coverage.
Actix Web Integration Snippet
use ;
use ;
use validate_token_hmac;
;
License
Dual-licensed under MIT or Apache-2.0. See LICENSE-MIT and LICENSE-APACHE for details.