Expand description
Zero-cost secure wrappers for secrets — Dynamic<T> for heap-allocated variable-length data,
Fixed<T> for stack-allocated fixed-size data.
This crate provides explicit, guarded wrappers for sensitive values (e.g. keys, tokens, ciphertexts)
with controlled exposure via .expose_secret() / .expose_secret_mut(). No accidental leaks via
Deref, AsRef, or implicit conversions.
§Quickstart
Add this to your Cargo.toml:
[dependencies]
secure-gate = "0.7.0-rc.14"Here’s a basic example with Fixed for a stack-allocated secret (no extra features needed):
use secure_gate::Fixed;
// Wrap a fixed-size secret (e.g., a 32-byte key)
let secret = Fixed::<[u8; 32]>::new([42; 32]);
// Expose it temporarily for use, zeroizing afterward
let value = secret.expose_secret(|exposed| {
// Do something with `exposed` (e.g., encrypt)
exposed[0]
});For dynamic secrets, enable the alloc feature and use Dynamic<T> similarly.
Check README.md for more examples and feature flags.
§Equality Options
ConstantTimeEq (via ct-eq feature): Direct byte-by-byte constant-time comparison using subtle.
Best for small/fixed-size secrets (< ~256–512 bytes) where speed matters most.
ConstantTimeEqExt (via ct-eq-hash feature): BLAKE3 hash -> constant-time compare on fixed 32-byte digest.
Faster for large/variable secrets (e.g. ML-KEM ciphertexts ~1–1.5 KiB, ML-DSA signatures ~2–4 KiB),
with length hiding and optional keyed mode (rand for per-process random key).
See the ConstantTimeEqExt trait documentation for performance numbers, security properties (probabilistic, timing-safe), and guidance on when to choose each (or hybrid).
Macros§
- dynamic_
alias - Creates a type alias for a dynamic-sized heap-allocated secure secret.
- dynamic_
generic_ alias - Creates a generic dynamic-sized heap-allocated secure secret type.
- fixed_
alias - Creates a type alias for a fixed-size stack-allocated secure secret.
- fixed_
generic_ alias - Creates a generic fixed-size stack-allocated secure secret type.
Structs§
- Dynamic
- Re-export of the
Dynamictype. Dynamic-sized heap-allocated secure secret wrapper. - Fixed
- Re-export of the
Fixedtype.
Enums§
- Base64
Error - Re-export of
Base64Errorfor convenience when using base64 decoding. Error type for Base64 decoding operations. - Bech32
Error - Re-export of
Bech32Errorfor convenience when using bech32 encoding/decoding. Error type for Bech32 operations (encoding and decoding). - Decoding
Error - Re-export of
DecodingErrorfor convenience in decoding operations. Unified error type for decoding operations across formats. - HexError
- Re-export of
HexErrorfor convenience when using hex decoding. Error type for Hex decoding operations.
Traits§
- Cloneable
Secret - Implement this trait on types that require safe duplication while maintaining security guarantees. The trait itself is a marker and does not provide methods, but implementations must ensure proper zeroization.
- Constant
Time Eq - Re-export of the traits. Trait for constant-time equality comparison to prevent timing attacks.
- Constant
Time EqExt - Expose
Secret - Trait for read-only access to secrets, including metadata.
- Expose
Secret Mut - Usage
- From
Base64 UrlStr - Extension trait for decoding URL-safe base64 strings to byte data.
- From
Bech32 Str - Extension trait for decoding Bech32 strings to byte data.
- From
Bech32m Str - From
HexStr - Extension trait for decoding hex strings to byte data.
- Secure
Decoding - Marker trait for types that support secure decoding operations.
- Secure
Encoding - Marker trait for types that support secure encoding operations.
- Serializable
Secret - Implement this on types that can be deliberately serialized while maintaining security. The trait itself is a marker and does not provide methods, but implementations must ensure that serialization does not leak secrets unintentionally.
- ToBase64
Url - Extension trait for encoding byte data to URL-safe base64 strings (no padding).
- ToBech32
- ToBech32m
- Extension trait for encoding byte data to Bech32m strings with a specified Human-Readable Part (HRP).
- ToHex
- Extension trait for encoding byte data to lowercase hexadecimal strings.