pub struct Vault { /* private fields */ }Expand description
A handle to an on-disk encrypted vault.
Operations follow an explicit lock/unlock protocol: Vault::unlock
derives the master key into zeroizing memory, every content operation
requires the unlocked state, and Vault::lock (plus Drop) scrubs it.
§Example
use krypton::Vault;
use std::path::Path;
let mut vault = Vault::new("myvault".into());
// First run: create the vault (fails if it already exists).
if !vault.exists() {
vault.init("correct horse battery staple")?;
}
vault.unlock("correct horse battery staple")?;
// Store a file and a whole directory tree.
vault.add(Path::new("report.pdf"), None)?; // stored as "report.pdf"
vault.add(Path::new("photos"), Some("pictures"))?; // tree renamed to "pictures"
for entry in vault.list()? {
println!("{} — {} bytes, dir: {}", entry.name, entry.size, entry.is_directory);
}
// Restore later; returns where the data was written.
let where_ = vault.extract("report.pdf", Path::new("./out"))?;
// Rotate the password without touching encrypted data.
vault.change_password("correct horse battery staple", "new secret")?;
vault.lock();Implementations§
Source§impl Vault
impl Vault
Sourcepub fn is_unlocked(&self) -> bool
pub fn is_unlocked(&self) -> bool
Whether the master key is currently held in memory.
Sourcepub fn init(&mut self, password: &str) -> Result<()>
pub fn init(&mut self, password: &str) -> Result<()>
Initializes a new vault protected by password.
Fails if the vault already exists. Created directories get owner-only permissions on Unix; the config is written atomically.
let mut vault = krypton::Vault::new("secrets".into());
vault.init("correct horse")?;Sourcepub fn unlock(&mut self, password: &str) -> Result<()>
pub fn unlock(&mut self, password: &str) -> Result<()>
Unlocks the vault with password.
Authentication failure reports Error::Authentication without
distinguishing wrong password from tampered config.
Sourcepub fn add(&mut self, source: &Path, name: Option<&str>) -> Result<()>
pub fn add(&mut self, source: &Path, name: Option<&str>) -> Result<()>
Adds a file or directory tree to the vault.
source- filesystem path to add (symlinks inside trees are skipped).name- explicit vault name; defaults to the source basename. Must not contain path separators or...
Adding a directory stores every child recursively; each child gets its own independently authenticated blob. An entry with the same name must not already exist (remove it first to replace). The manifest is committed last and atomically, so a crash can leave orphaned blobs but never a dangling index.
let mut vault = krypton::Vault::new("secrets".into());
vault.unlock("correct horse")?;
vault.add(std::path::Path::new("notes.txt"), None)?;
vault.add(std::path::Path::new("~/docs"), Some("docs"))?; // whole treeSourcepub fn remove(&mut self, name: &str) -> Result<()>
pub fn remove(&mut self, name: &str) -> Result<()>
Removes an entry (and, for directories, its whole subtree).
Both the ciphertext blobs and their manifest records are deleted.
Sourcepub fn list(&self) -> Result<Vec<EntryInfo>>
pub fn list(&self) -> Result<Vec<EntryInfo>>
Lists top-level entries. Children of directories are addressed by
their full parent/child path in other operations.
Sourcepub fn extract(&self, name: &str, dest: &Path) -> Result<PathBuf>
pub fn extract(&self, name: &str, dest: &Path) -> Result<PathBuf>
Extracts an entry.
- Files are written to
dest(temp file + rename). - Directories are recreated under
destwith their full subtree.
Returns the output location actually used. Child names recovered from
the vault are sanitized before being joined onto dest; any traversal
attempt aborts the operation.
let vault = krypton::Vault::new("secrets".into());
let written = vault.extract("docs", std::path::Path::new("./restored"))?;
println!("restored to {}", written.display());Sourcepub fn change_password(
&mut self,
old_password: &str,
new_password: &str,
) -> Result<()>
pub fn change_password( &mut self, old_password: &str, new_password: &str, ) -> Result<()>
Changes the vault password after verifying old_password.
Only the wrapped master key is re-encrypted; stored data is untouched. Works regardless of lock state and leaves the lock state unchanged.
let mut vault = krypton::Vault::new("secrets".into());
vault.change_password("old horse", "new horse")?;Sourcepub fn verify(&mut self, password: &str) -> Result<IntegrityReport>
pub fn verify(&mut self, password: &str) -> Result<IntegrityReport>
Verifies every entry’s blob by fully decrypting it (authenticating all chunk tags), reporting missing or corrupted items.
Restores the prior lock state when done.
let mut vault = krypton::Vault::new("secrets".into());
let report = vault.verify("correct horse")?;
if report.missing.is_empty() && report.corrupted.is_empty() {
println!("all {} entries authenticated", report.total_entries);
} else {
eprintln!("missing: {:?}, corrupted: {:?}", report.missing, report.corrupted);
}