mirror of
https://github.com/aljazceru/pubky-core.git
synced 2026-01-30 19:34:32 +01:00
feat(pubky): add recovery file generation and decryption
This commit is contained in:
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user