feat(pubky): add recovery file generation and decryption

This commit is contained in:
nazeh
2024-08-03 16:33:38 +03:00
parent 5ee464a5b4
commit 11334bf81c
7 changed files with 129 additions and 13 deletions

View File

@@ -30,10 +30,7 @@ pub fn random_bytes<const N: usize>() -> [u8; N] {
arr
}
pub fn encrypt(
plain_text: &[u8],
encryption_key: &[u8; 32],
) -> Result<Vec<u8>, crypto_secretbox::Error> {
pub fn encrypt(plain_text: &[u8], encryption_key: &[u8; 32]) -> Result<Vec<u8>, Error> {
let cipher = XSalsa20Poly1305::new(encryption_key.into());
let nonce = XSalsa20Poly1305::generate_nonce(&mut OsRng); // unique per message
let ciphertext = cipher.encrypt(&nonce, plain_text)?;
@@ -45,12 +42,16 @@ pub fn encrypt(
Ok(out)
}
pub fn decrypt(
bytes: &[u8],
encryption_key: &[u8; 32],
) -> Result<Vec<u8>, crypto_secretbox::Error> {
pub fn decrypt(bytes: &[u8], encryption_key: &[u8; 32]) -> Result<Vec<u8>, Error> {
let cipher = XSalsa20Poly1305::new(encryption_key.into());
cipher.decrypt(bytes[..24].into(), &bytes[24..])
Ok(cipher.decrypt(bytes[..24].into(), &bytes[24..])?)
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
SecretBox(#[from] crypto_secretbox::Error),
}
#[cfg(test)]