URICrypt
A Rust library for encrypting URIs while preserving their hierarchical structure and common prefixes.
Features
- Prefix-Preserving Encryption: URIs with shared paths maintain identical encrypted prefixes, enabling efficient caching and storage
- Plaintext Scheme: URI schemes (like
https://) remain unencrypted for protocol identification - Deterministic Encryption: Same inputs always produce the same encrypted output
- URL-Safe Output: Generates clean URLs without padding characters using base64 URL-safe encoding
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Example
use ;
Prefix Preservation
URIs sharing common paths will have identical encrypted prefixes:
let key = b"secret-key";
let ctx = b"app-context";
let uri1 = "https://api.example.com/v1/users/123";
let uri2 = "https://api.example.com/v1/users/456";
let uri3 = "https://api.example.com/v2/posts";
let enc1 = encrypt_uri;
let enc2 = encrypt_uri;
let enc3 = encrypt_uri;
// All three start with "https://" (plaintext scheme)
// enc1 and enc2 share the same encrypted prefix for "api.example.com/v1/users/"
// All three share the encrypted prefix for "api.example.com/"
API Reference
encrypt_uri
Encrypts a URI while preserving its hierarchical structure and keeping the scheme in plaintext.
Parameters:
uri: The URI to encrypt (must contain "://")secret_key: Secret key for encryption (use at least 32 bytes)context: Additional context for domain separation (e.g., app version)
Returns: URI with plaintext scheme followed by encrypted components
decrypt_uri
Decrypts a URI encrypted with encrypt_uri.
Parameters:
encrypted_uri: The encrypted URI with plaintext schemesecret_key: Same secret key used for encryptioncontext: Same context used for encryption
Returns: Ok(String) with the original URI, or Err(String) if decryption fails
Security Considerations
- Key Management: Use a cryptographically secure random key of at least 32 bytes
- Context Binding: The context parameter provides domain separation - use it to bind encryption to specific applications or versions
- Deterministic: This is deterministic encryption - identical URIs encrypted with the same key/context produce identical ciphertexts
- Authentication: Each URI component includes a 16-byte authentication tag (SIV) that prevents tampering
- Chained SIVs: Each component's SIV includes all previous SIVs in its computation for stronger authentication
- Plaintext Scheme: URI schemes remain unencrypted for protocol identification
- Algorithm: Uses TurboShake128 (SHA-3 family) for key derivation and stream generation
Use Cases
- Privacy-Preserving Caching: Cache encrypted URLs while maintaining cache hierarchy
- Log Anonymization: Store and analyze sensitive URLs in logs without exposing actual endpoints
- Compliant Data Storage: Meet data residency requirements while maintaining URL structure