Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

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