| Crates.io | nonce-auth |
| lib.rs | nonce-auth |
| version | 0.6.2 |
| created_at | 2025-05-27 10:01:49.43852+00 |
| updated_at | 2025-08-05 07:31:02.868822+00 |
| description | A secure nonce-based authentication library with pluggable storage backends |
| homepage | https://github.com/kookyleo/nonce-auth |
| repository | https://github.com/kookyleo/nonce-auth |
| max_upload_size | |
| id | 1690864 |
| size | 488,832 |

A lightweight, secure nonce-based authentication library for Rust, designed to prevent replay attacks in APIs and other services.
cargo add nonce-auth tokio
use nonce_auth::{CredentialBuilder, CredentialVerifier, storage::MemoryStorage, storage::NonceStorage};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Shared secret between credential creator and verifier
let secret = b"my-super-secret-key";
let payload = b"important_api_request_data";
// Create storage backend (in-memory for this example)
let storage: Arc<dyn NonceStorage> = Arc::new(MemoryStorage::new());
// 1. Create a credential
let credential = CredentialBuilder::new(secret)
.sign(payload)?;
println!("✅ Generated credential with nonce: {}", credential.nonce);
// 2. Verify the credential
CredentialVerifier::new(Arc::clone(&storage))
.with_secret(secret)
.verify(&credential, payload)
.await?;
println!("✅ First verification successful!");
// 3. Replay attack is automatically rejected
let replay_result = CredentialVerifier::new(storage)
.with_secret(secret)
.verify(&credential, payload)
.await;
assert!(replay_result.is_err());
println!("✅ Replay attack correctly rejected!");
Ok(())
}
For more advanced usage, see examples and User Guide.
MemoryStorage): Fast, built-in, perfect for single-instance applicationsRedisStorage): Distributed, production-ready, with connection pooling (feature: redis-storage)SQLiteStorage): Supports WAL mode, with connection pooling (feature: sqlite-storage)NonceStorage trait for your own backendThe library provides several configuration approaches:
ConfigPreset::Production, ConfigPreset::Development, ConfigPreset::HighSecurityNONCE_AUTH_STORAGE_TTL, NONCE_AUTH_DEFAULT_TIME_WINDOWFor detailed configuration options, see User Guide.
simple.rs - Basic credential creation and verificationweb.rs - Web demosqlite_storage.rs - SQLite storage backendredis_example.rs - Redis with connection poolingperformance_test.rs - Performance benchmarkingLicensed under either of
at your option.