cryptex 3.0.0

Store and retrieve secrets in OS keyrings, encrypted SQLite or Turso files, in-memory and browser keyrings, or under a YubiHSM 2 or Nitrokey 3
//! Store, list, rekey and delete secrets in an encrypted VFS keyring.
//!
//! ```bash
//! cargo run --example file_keyring --no-default-features --features encrypted-vfs
//! ```

#[cfg(feature = "encrypted-vfs")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    use cryptex::KeyRing;
    use cryptex::encrypted_vfs::{ConnectionParams, EncryptedVfsKeyring};

    // A scratch directory, so the example never touches ~/.cryptex.
    let dir = std::env::temp_dir().join(format!("cryptex-example-{}", std::process::id()));

    // The salt, cipher and Argon2 costs are stored in the file's header, so
    // only the password is needed to open it again.
    let params: ConnectionParams = "password='correct horse battery staple'".parse()?;
    let mut keyring = EncryptedVfsKeyring::with_params(&params, Some(dir.clone()))?;
    keyring.set_secret("aws", b"AKIAEXAMPLE")?;
    keyring.set_secret("github", b"ghp_example")?;
    println!("ids: {:?}", keyring.keys()?);

    // Rekey to a new password and AES-256-GCM; the old password stops working.
    let new_params: ConnectionParams =
        "password='a different passphrase' cipher=aes256gcm".parse()?;
    keyring.rekey(&new_params)?;
    drop(keyring);

    let mut keyring = EncryptedVfsKeyring::with_params(&new_params, Some(dir.clone()))?;
    let aws = keyring.get_secret("aws")?;
    println!("aws: {}", String::from_utf8_lossy(aws.as_slice()));
    keyring.delete_secret("github")?;
    println!("ids after delete: {:?}", keyring.keys()?);
    drop(keyring);

    std::fs::remove_dir_all(dir)?;
    Ok(())
}

#[cfg(not(feature = "encrypted-vfs"))]
fn main() {
    eprintln!("Run with --features encrypted-vfs");
}