pub struct SecretKey { /* private fields */ }Expand description
A validated age secret key protected by memory zeroization.
SecretKey wraps the raw key string inside Zeroizing, which guarantees
that the memory is securely erased when the value is dropped. This prevents
secrets from lingering in memory dumps or swap files.
§Validation
The key is validated at construction time via new:
- It must be non‑empty.
- It must start with the string
AGE-SECRET-KEY-1(case‑sensitive).
§Security properties
- Redacted display –
DisplayandDebugprint[REDACTED], never the actual key. - Zeroization on drop – memory is overwritten with zeros when the
SecretKey(or any clone) is dropped. - Cloneable – cloning creates a new independent
Zeroizingcopy that is also zeroized separately.
§Examples
use age_setup::SecretKey;
let sk = SecretKey::new("AGE-SECRET-KEY-1mytestkey".into())?;
println!("{}", sk); // prints: [REDACTED]
println!("{:?}", sk); // prints: SecretKey { ... [REDACTED] ... }
let raw = sk.expose_secret(); // careful: raw secret exposedImplementations§
Source§impl SecretKey
impl SecretKey
Sourcepub fn new(raw: String) -> Result<Self>
pub fn new(raw: String) -> Result<Self>
Creates a new SecretKey after validating the raw string.
§Validation checks
- The key must not be empty.
- The key must start with
"AGE-SECRET-KEY-1".
§Errors
Returns Error::Validation with a
descriptive reason if any check fails.
§Examples
let valid = SecretKey::new("AGE-SECRET-KEY-1abc".into()).unwrap();
let empty = SecretKey::new("".into());
assert!(empty.is_err());
let wrong_prefix = SecretKey::new("ssh-rsa ...".into());
assert!(wrong_prefix.is_err());Sourcepub fn expose_secret(&self) -> &str
pub fn expose_secret(&self) -> &str
Exposes the raw secret key string.
⚠️ Security Warning – this method returns the actual secret material
as a &str. Only use it when absolutely necessary (e.g., to pass the
key to an age decryption function or to write it to a securely
permissioned file). Avoid logging, printing, or storing the returned
string in an unsecured location.
§Examples
let sk = SecretKey::new("AGE-SECRET-KEY-1test".into()).unwrap();
let raw = sk.expose_secret();
assert_eq!(raw, "AGE-SECRET-KEY-1test");