use std::collections::HashMap;
use rustcrypt_jasypt::{is_encrypted, Encryptor};
fn main() -> rustcrypt_jasypt::Result<()> {
let enc = Encryptor::new("my-secret-password")?;
let wrapped = enc.encrypt_with_prefix("db_password_123")?;
println!("Encrypted : {wrapped}");
println!("Decrypted : {}", enc.decrypt_prefixed(&wrapped)?);
let raw = enc.encrypt("db_password_123")?;
println!("Raw : {raw}");
println!("Decrypted : {}", enc.decrypt(&raw)?);
println!("is_encrypted(wrapped) = {}", is_encrypted(&wrapped));
println!("is_encrypted(plain) = {}", is_encrypted("plain-text"));
let api_key = enc.encrypt_with_prefix("sk-live-123")?;
let text = format!("DB_HOST=localhost\nDB_PASS={wrapped}\nAPI_KEY={api_key}\n");
println!(
"\n--- decrypt_all_in_string ---\n{}",
enc.decrypt_all_in_string(&text)?
);
let mut config = HashMap::new();
config.insert("host".to_owned(), "localhost".to_owned());
config.insert("password".to_owned(), wrapped.clone());
let decrypted = enc.decrypt_map(&config)?;
println!("--- decrypt_map ---");
println!("host = {}", decrypted["host"]);
println!("password = {}", decrypted["password"]);
let tuned = Encryptor::new("my-secret-password")?
.with_iterations(50_000)
.with_salt_size(32)
.with_key_size(32)?;
let value = tuned.encrypt_with_prefix("tuned")?;
assert_eq!(tuned.decrypt_prefixed(&value)?, "tuned");
assert!(enc.decrypt_prefixed(&value).is_err());
println!("\nCustom-parameter round trip OK");
Ok(())
}