this repo has no description
at wasm 37 lines 1.2 kB view raw
1use thiserror::Error; 2 3pub fn encrypt_with_passphrase( 4 message: &[u8], 5 passphrase: &str, 6) -> Result<String, age::EncryptError> { 7 let passphrase = age::secrecy::SecretString::from(passphrase); 8 let recipient = age::scrypt::Recipient::new(passphrase.clone()); 9 10 let encrypted = age::encrypt_and_armor(&recipient, message)?; 11 12 Ok(encrypted) 13} 14 15// the function `decrypt_with_passphrase` has two possible failure cases: 16// - when decryption fails 17// - when the data was decrypted succesfully but the result is not UTF-8 valid 18#[derive(Error, Debug)] 19pub enum DecryptError { 20 #[error("unable to decrypt message: {0}")] 21 Decrypt(#[from] age::DecryptError), 22 #[error("decrypted message is not UTF-8 valid: {0}")] 23 Io(#[from] std::string::FromUtf8Error), 24} 25 26pub fn decrypt_with_passphrase( 27 encrypted_message: &[u8], 28 passphrase: &str, 29) -> Result<String, DecryptError> { 30 let passphrase = age::secrecy::SecretString::from(passphrase); 31 let identity = age::scrypt::Identity::new(passphrase); 32 33 let decrypted = age::decrypt(&identity, encrypted_message)?; 34 let decrypted = String::from_utf8(decrypted)?; 35 36 Ok(decrypted) 37}