1pub mod caesar;
2pub mod base64_wrapper;
3pub mod sodium_wrapper;
4
5use caesar::{encrypt_caesar, decrypt_caesar};
6use sodium_wrapper::{decrypt_sodium, encrypt_sodium};
7use base64_wrapper::{encode_base64, decode_base64};
8
9pub fn encrypt(plaintext: &str, caesar_shift: u8) -> Result<(String, String, String), String> {
12 let caesar_cipher_text = encrypt_caesar(plaintext, caesar_shift);
14
15 let (ciphertext, nonce, key) = encrypt_sodium(&caesar_cipher_text)?;
17
18 let ciphertext_b64 = encode_base64(&ciphertext);
20 let nonce_b64 = encode_base64(nonce.as_ref());
21 let key_b64 = encode_base64(key.as_ref());
22
23 Ok((ciphertext_b64, nonce_b64, key_b64))
24}
25
26pub fn decrypt(ciphertext_b64: &str, nonce_b64: &str, key_b64: &str, caesar_shift: u8) -> Result<String, String> {
29 let ciphertext = decode_base64(ciphertext_b64)
31 .map_err(|e| e.to_string())?;
32 let nonce = decode_base64(nonce_b64)
33 .map_err(|e| e.to_string())?;
34 let key = decode_base64(key_b64)
35 .map_err(|e| e.to_string())?;
36
37 let nonce = sodiumoxide::crypto::secretbox::Nonce::from_slice(&nonce)
39 .ok_or("Nonce is not the correct length")?;
40 let key = sodiumoxide::crypto::secretbox::Key::from_slice(&key)
41 .ok_or("Key is not the correct length")?;
42
43 let decrypted_text = decrypt_sodium(&ciphertext, &nonce, &key)?;
45
46 let plaintext = decrypt_caesar(&decrypted_text, caesar_shift);
48
49 Ok(plaintext)
50}