From 2dbf7ed9c2be87e08fb395725e665d06bdda3489 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 23 Jun 2024 14:06:27 +0200 Subject: [PATCH] add tests for encryption --- src/encrypt.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/encrypt.rs b/src/encrypt.rs index 25b9ceb..1de7dec 100644 --- a/src/encrypt.rs +++ b/src/encrypt.rs @@ -207,3 +207,39 @@ pub fn decrypt( let key = aes_gcm::Key::::from_slice(key.as_slice()); Aes256Gcm::new(key).decrypt(&nonce, encrypted) } + +#[cfg(test)] +mod test { + use super::*; + use crate::{Chain, SsiSecret}; + + #[test] + fn aes_roundcrypt() { + let key = "Some key"; + let source = b"Message to encrypt"; + + let (nonce, encrypted) = encrypt(source.to_vec(), key); + let decrypted = decrypt(&encrypted, nonce, key).unwrap(); + assert_eq!(decrypted, source); + } + + #[test] + fn ed25519_keycrypt() { + let sk = SsiSecret::new(Algo::Ed25519, Chain::Bitcoin); + let pair = SsiPair::from(sk); + let key = SymmetricKey::new(); + let encrypted = pair.pk.encrypt_key(&key).unwrap(); + let decrypted = pair.decrypt_key(encrypted).unwrap(); + assert_eq!(key.0, decrypted.0); + } + + #[test] + fn ed25519_roundcrypt() { + let key = SsiSecret::new(Algo::Ed25519, Chain::Bitcoin); + let source = b"Message to encrypt"; + + let encrypted = Encrypted::encrypt(source.to_vec(), [key.to_public()]).unwrap(); + let decrypted = encrypted.decrypt(key).unwrap(); + assert_eq!(decrypted, source); + } +}