Crates.io | uricrypt |
lib.rs | uricrypt |
version | 0.1.9 |
created_at | 2025-09-17 12:29:13.212867+00 |
updated_at | 2025-09-18 12:25:57.025455+00 |
description | Hierarchy-preserving deterministic URI encryption |
homepage | https://github.com/jedisct1/rust-uricrypt |
repository | https://github.com/jedisct1/rust-uricrypt |
max_upload_size | |
id | 1843199 |
size | 51,498 |
A Rust library for encrypting URIs while preserving their hierarchical structure and common prefixes.
https://
) remain unencrypted for protocol identification/path/to/file
)Add this to your Cargo.toml
:
[dependencies]
uricrypt = "0.1"
use uricrypt::{encrypt_uri, decrypt_uri};
fn main() {
let uri = "https://example.com/api/v1/users";
let secret_key = b"your-secret-key-min-32-bytes-recommended";
let context = b"MyApp-v1.0";
// Encrypt the URI (scheme remains plaintext)
let encrypted = encrypt_uri(uri, secret_key, context);
println!("Encrypted: {}", encrypted);
// Output: https://<base64-encoded-encrypted-components>
// Decrypt it back
let decrypted = decrypt_uri(&encrypted, secret_key, context).unwrap();
assert_eq!(uri, decrypted);
// Also works with path-only URIs
let path = "/api/v1/users";
let encrypted_path = encrypt_uri(path, secret_key, context);
println!("Encrypted path: {}", encrypted_path);
// Output: /<base64-encoded-encrypted-path>
}
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(uri1, key, ctx);
let enc2 = encrypt_uri(uri2, key, ctx);
let enc3 = encrypt_uri(uri3, key, ctx);
// 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/"
encrypt_uri
pub fn encrypt_uri(uri: &str, secret_key: &[u8], context: &[u8]) -> String
Encrypts a URI while preserving its hierarchical structure and keeping the scheme in plaintext.
Parameters:
uri
: The URI to encrypt (with or without scheme)secret_key
: Secret key for encryption (use at least 32 bytes)context
: Additional context for domain separation (e.g., app version)Returns:
decrypt_uri
pub fn decrypt_uri(
encrypted_uri: &str,
secret_key: &[u8],
context: &[u8],
) -> Result<String, String>
Decrypts a URI encrypted with encrypt_uri
.
Parameters:
encrypted_uri
: The encrypted URI (with plaintext scheme or path-only format)secret_key
: Same secret key used for encryptioncontext
: Same context used for encryptionReturns: Ok(String)
with the original URI, or Err(String)
if authentication fails