| Crates.io | secret-utils |
| lib.rs | secret-utils |
| version | 0.2.2 |
| created_at | 2025-10-03 15:34:54.029545+00 |
| updated_at | 2025-10-04 11:02:38.23028+00 |
| description | Shared utilities for secret handling (wrappers, zeroization, secrecy) used across the PAKEs-Conflux workspace |
| homepage | |
| repository | https://github.com/thatnewyorker/PAKEs-Conflux |
| max_upload_size | |
| id | 1866840 |
| size | 32,203 |
Zeroizing secret wrappers and utilities used across the PAKEs-Conflux workspace.
This crate provides lightweight, no-unsafe wrappers for secret material (passwords, session keys, etc.) with:
AsRef<[u8]>, deref to &[u8]) to minimize copies.ct_eq).It is designed to be small, straightforward, and compatible with no_std + alloc.
unsafe codealloc (default): Enables heap-backed wrappers (required for SecretBytes and SecretKey).std: Convenience alias; implies alloc.If you disable default features, the wrappers will be unavailable (docs-only build).
Basic (default features include alloc):
[dependencies]
secret-utils = "0.2"
no_std with alloc:
[dependencies]
secret-utils = { version = "0.2", default-features = false, features = ["alloc"] }
no_std docs-only (wrappers disabled):
[dependencies]
secret-utils = { version = "0.2", default-features = false }
SecretBytesDebug: prints [redacted] and length.AsRef<[u8]> and deref to &[u8] for borrow-first access.Clone.SecretBytes::new(Vec<u8>) -> SelfFrom<Vec<u8>> for SecretBytesinto_inner(self) -> Vec<u8> (explicit escape hatch; see Security Notes)SecretKeyDebug: prints [redacted] and length.AsRef<[u8]> and deref to &[u8] for borrow-first access.Clone.PartialEq. Use ct_eq(&other) for explicit, best-effort constant-time equality.SecretKey::new(Vec<u8>) -> SelfFrom<Vec<u8>> for SecretKeyinto_inner(self) -> Vec<u8> (explicit escape hatch; see Security Notes)Borrow-first access (avoid copying):
use secret_utils::wrappers::SecretKey;
fn use_key_material(key: &SecretKey) {
// Borrow key bytes without copying
let key_bytes: &[u8] = key.as_ref();
// Use with your AEAD/KDF/HKDF, etc.
// For example:
// aead.init(key_bytes, nonce, associated_data);
}
Compare keys with explicit equality:
use secret_utils::wrappers::SecretKey;
fn keys_equal(a: &SecretKey, b: &SecretKey) -> bool {
// Best-effort constant-time comparison
a.ct_eq(b)
}
Hex-encode (only when necessary):
use hex;
use secret_utils::wrappers::SecretKey;
fn key_hex(key: &SecretKey) -> String {
hex::encode(key.as_ref())
}
Passwords with SecretBytes:
use secret_utils::wrappers::SecretBytes;
fn authenticate(password: &SecretBytes) {
// Borrow password bytes without copying
let pw = password.as_ref();
// Use pw in your password hashing / verifier logic
}
Explicit extraction when absolutely required:
use secret_utils::wrappers::SecretKey;
// Prefer borrowing via `as_ref()`; extraction should be rare and deliberate.
fn export_for_storage(mut key: SecretKey) -> Vec<u8> {
// Transfers ownership of the sensitive bytes to the caller
// Caller is responsible for careful handling post-extraction
key.into_inner()
}
Zeroize and ZeroizeOnDrop so memory is cleared when dropped and when explicitly zeroized.Debug never includes raw bytes; only [redacted] with length.PartialEq for SecretKey: comparing secrets should be explicit; ct_eq avoids accidental timing leaks that could arise with naive equality.AsRef<[u8]>/deref encourage minimal copies when interfacing with crypto APIs that accept &[u8].ct_eq is a best-effort constant-time comparison for byte arrays. Compiler optimizations, CPU details, and surrounding logic can still influence timing; design higher-level protocols to minimize the need to compare secrets directly.Debug is a safety net, not a license to print secrets.&[u8] over taking ownership. Avoid cloning or persisting raw secret bytes.into_inner is an explicit escape hatch that transfers secret ownership. Use rarely and deliberately.0.2:
SecretKey: removed PartialEq; added ct_eq(&Self) -> bool.Debug for SecretBytes for consistent behavior with SecretKey.cfg gating for Debug impls to match alloc feature.into_inner now uses core::mem::take for clarity.Licensed under either of:
at your option.