VibeProtocol/
lib.rs

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
9/// Encrypts a plaintext message using the VibeProtocol encryption sequence:
10/// Caesar cipher -> Sodium encryption -> Base64 encoding.
11pub fn encrypt(plaintext: &str, caesar_shift: u8) -> Result<(String, String, String), String> {
12    // Step 1: Caesar cipher
13    let caesar_cipher_text = encrypt_caesar(plaintext, caesar_shift);
14
15    // Step 2: Sodium encryption
16    let (ciphertext, nonce, key) = encrypt_sodium(&caesar_cipher_text)?;
17
18    // Step 3: Base64 encoding
19    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
26/// Decrypts a ciphertext message using the VibeProtocol decryption sequence:
27/// Base64 decoding -> Sodium decryption -> Caesar cipher.
28pub fn decrypt(ciphertext_b64: &str, nonce_b64: &str, key_b64: &str, caesar_shift: u8) -> Result<String, String> {
29    // Step 1: Base64 decoding with error mapping
30    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    // Ensure nonce and key are the correct length
38    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    // Step 2: Sodium decryption
44    let decrypted_text = decrypt_sodium(&ciphertext, &nonce, &key)?;
45
46    // Step 3: Caesar cipher decryption
47    let plaintext = decrypt_caesar(&decrypted_text, caesar_shift);
48
49    Ok(plaintext)
50}