use db_keystore::{DbKeyStore, DbKeyStoreConfig, EncryptionOpts};
use keyring_core::api::CredentialStoreApi;
use std::{collections::HashMap, path::Path, sync::Arc};
use zeroize::Zeroizing;
const CIPHER: &str = "aegis256";
const HEXKEY: &str = "0000000011111111222222223333333344444444555555556666666677777777";
const SERVICE: &str = "enc_demo";
fn create_db_config(
path: &Path,
cipher: &str,
hexkey: &str,
) -> Result<Arc<DbKeyStore>, keyring_core::Error> {
let encryption_opts = EncryptionOpts::new(cipher, hexkey);
let config = DbKeyStoreConfig {
path: path.to_owned(),
encryption_opts: Some(encryption_opts),
..Default::default()
};
DbKeyStore::new(config)
}
fn create_db_modifiers(
path: &Path,
cipher: &str,
hexkey: &str,
) -> Result<Arc<DbKeyStore>, keyring_core::Error> {
let path = path.to_str().expect("path is utf8");
let settings = HashMap::from([("path", path), ("cipher", cipher), ("hexkey", hexkey)]);
DbKeyStore::new_with_modifiers(&settings)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let user = "alice";
let password = Zeroizing::new("dromomeryx".to_string());
let db_path = tempfile::tempdir()?.path().join("encrypted_keystore.db");
{
let store = create_db_config(&db_path, CIPHER, HEXKEY)?;
let entry = store.build(SERVICE, user, None)?;
entry.set_password(password.as_str())?;
println!("saved password");
}
{
let store = create_db_modifiers(&db_path, CIPHER, HEXKEY)?;
let results = store.search(&HashMap::from([("service", SERVICE), ("user", user)]))?;
assert_eq!(results.len(), 1);
let first = results.first().expect("found password");
let fetched = Zeroizing::new(first.get_password()?);
assert_eq!(fetched.as_str(), password.as_str());
if fetched.as_str() == password.as_str() {
println!("passwords match!")
}
}
Ok(())
}