secure-gate
no_std-compatible wrappers for sensitive data with explicit exposure requirements.
Fixed<T>— Stack-allocated wrapperDynamic<T>— Heap-allocated wrapperFixedRng<N>— Cryptographically secure random bytes of fixed length NDynamicRng— Heap-allocated cryptographically secure random bytesHexString— Validated lowercase hexadecimal string wrapperBase64String— Validated URL-safe base64 string wrapper (no padding)
With the zeroize feature enabled, memory containing secrets is zeroed on drop, including spare capacity where applicable.
Access to secret data requires an explicit .expose_secret() call. There are no Deref implementations or other implicit access paths.
Cloning is opt-in via the CloneableSecret trait.
Installation
[]
= "0.7.0-rc.1"
Recommended configuration:
= { = "0.7.0-rc.1", = ["full"] }
Features
| Feature | Description |
|---|---|
zeroize |
Memory zeroing on drop and opt-in cloning via CloneableSecret |
rand |
Random generation (FixedRng<N>::generate(), DynamicRng::generate()) |
ct-eq |
Constant-time equality comparison |
encoding |
Encoding support (encoding-hex + encoding-base64) |
encoding-hex |
Hex encoding, HexString, FixedRng hex methods |
encoding-base64 |
Base64String |
full |
All optional features |
The crate is no_std-compatible with alloc. Features are optional and add no overhead when unused.
Quick Start
use ;
fixed_alias!;
dynamic_alias!;
let pw: Password = "hunter2".into;
assert_eq!;
Opt-In Cloning
Cloning is not implemented by default. It is enabled only for types that implement CloneableSecret (requires the zeroize feature).
Blanket implementations exist for primitives and fixed-size arrays.
Randomness
FixedRng<N> can only be constructed via cryptographically secure RNG.
Direct generation is also available:
Encoding
Encoding functions require explicit .expose_secret(). Invalid inputs to HexString::new and Base64String::new are zeroed when the zeroize feature is enabled.
Constant-Time Equality
Available on Fixed<[u8; N]> and Dynamic<T> where T: AsRef<[u8]>.
Macros
use ;
fixed_alias!;
dynamic_alias!;
Memory Guarantees (zeroize enabled)
| Type | Allocation | Auto-zero | Full wipe | Slack eliminated | Notes |
|---|---|---|---|---|---|
Fixed<T> |
Stack | Yes | Yes | Yes (no heap) | |
Dynamic<T> |
Heap | Yes | Yes | No (until drop) | Use shrink_to_fit() |
FixedRng<N> |
Stack | Yes | Yes | Yes | |
HexString |
Heap | Yes (invalid input) | Yes | No (until drop) | Validated hex |
Base64String |
Heap | Yes (invalid input) | Yes | No (until drop) | Validated base64 |
Performance
The wrappers add no runtime overhead compared to raw types in benchmarks.
Changelog
License
MIT OR Apache-2.0