API Keys Simplified
A secure, Rust library for generating and validating API keys with built-in security best practices.
What It Does
- Generate cryptographically secure API keys (192-bit entropy default)
- Hash keys using Argon2id (memory-hard, OWASP recommended)
- Verify keys with constant-time comparison (prevents timing attacks)
- Protect sensitive data with automatic memory zeroing
Quick Start
Installation
[]
= "0.1"
Basic Usage
use ;
Key Format
prefix.environment.random_data.checksum
│ │ │ │
│ │ │ └─ CRC32 (optional)
│ │ └─────────── Base64URL (192 bits)
│ └──────────────────────── dev/test/staging/live
└────────────────────────────────── User-defined (e.g., acme_sk, stripe_pk)
Examples:
gpmcp_sk.live.Xf8kP2qW9zLmN4vC8aH5tJw1bQmK3rN9.a1b2c3d4acme_api.dev.Rt7jK3pV8wNmQ2uD4fG6hLk8nPqS2uW5.e5f6g7h8
Why Use This?
Common API key security mistakes:
❌ Weak random number generators → Predictable keys
❌ Plaintext storage → Database breach = total compromise
❌ Vulnerable hashing (MD5, SHA1) → Easy to crack
❌ Timing-vulnerable comparisons → Leaks key information
❌ Keys lingering in memory → Core dumps expose secrets
This library solves all of these with secure defaults and minimal code.
Security Features
🔒 Cryptographic Strength
- RNG: OS-level CSPRNG via
getrandomcrate - Hashing: Argon2id (Password Hashing Competition winner)
- Entropy: 192 bits default (NIST compliant through 2030+)
- Memory-Hard: Prevents GPU/ASIC brute force attacks
🛡️ Side-Channel Protection
- Constant-Time Comparison: Via
subtlecrate (timing-attack resistant) - No Early Returns: Same verification time regardless of key differences
- Memory Hardness: Argon2 prevents cache-timing attacks
🔐 Memory Safety
- Auto-Zeroing:
SecureStringclears memory on drop viazeroizecrate - No Accidental Logging: Custom
Debugimpl redacts keys - Explicit Access: No
Dereftrait (prevents silent leaks)
📊 DoS Protection
- Input Validation: 512-byte max key length
- Resource Limits: Prevents hash complexity attacks
- Fast Rejection: Optional CRC32 checksum for malformed keys
🔍 Threat Model
Protected Against: ✅ Brute force • ✅ Timing attacks • ✅ Rainbow tables • ✅ Memory disclosure • ✅ Database breaches • ✅ GPU/ASIC attacks
NOT Protected Against: ❌ Compromised app server • ❌ User negligence • ❌ Network interception (use HTTPS) • ❌ Quantum computers
Best Practices
// ✅ Never log keys (auto-redacted)
println!; // Prints: ApiKey { key: "[REDACTED]", ... }
// ✅ Show keys only once
let key = generate_default ?;
display_to_user_once;
db.save; // Store hash only
// ✅ Always use HTTPS
let response = client.get
.header
.send ?;
// ✅ Implement key rotation
// ✅ Rate limit verification
if rate_limiter.check.is_err
verify ?;
Performance
| Preset | Memory | Time | Verification |
|---|---|---|---|
| Balanced (default) | 19 MB | 2 iter | ~50ms |
| High Security | 64 MB | 3 iter | ~150ms |
Note: Slow verification is intentional—it prevents brute force attacks.
Testing
Error Handling
use ;
match generate_default
Error messages are intentionally generic to prevent information leakage.
Comparison
| Feature | api-keys-simplified | uuid | nanoid |
|---|---|---|---|
| Cryptographic security | ✅ Argon2id | ❌ | ⚠️ Basic |
| Hashed storage | ✅ Built-in | ❌ | ❌ |
| Constant-time verify | ✅ Yes | ❌ | ❌ |
| Memory protection | ✅ Auto-zeroing | ❌ | ❌ |
| Structured format | ✅ prefix.env.data | ❌ | ❌ |
License
Licensed under the Apache License, Version 2.0.
Dependencies
All cryptographic implementations use well-audited crates:
argon2- Official Argon2 implementationsubtle- Constant-time primitiveszeroize- Secure memory zeroinggetrandom- OS-level CSPRNG
References
- OWASP Authentication Cheat Sheet
- NIST SP 800-63B - Digital Identity Guidelines
- RFC 9106 - Argon2 Specification
Reporting Vulnerabilities
Email security issues to: sandip@ssdd.dev