ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use crate::crypto_systems::ceasar::Ceasar;
use crate::error::CeasarError;
use crate::prelude::encode_char;
use crate::prelude::ALPHABET_LEN;
use crate::utils::{decode_digit, BaseString, EncodedString, StringType};
use crate::Traits::Decrypt;

use rayon::prelude::*;

impl Decrypt<CeasarError, BaseString, BaseString> for Ceasar {
    /// Decrypts a string using the Caesar cipher.
    ///
    /// This function iterates over each character in the input string, converts it to lowercase,
    /// and then calls the `decrypt_char` function on it. The results are collected into a `String`.
    ///
    /// # Arguments
    ///
    /// * `cipher_text` - A `String` that holds the text to be decrypted.
    ///
    /// # Returns
    ///
    /// * A `Result<String, CeasarError>` which is `Ok` if the decryption is successful, and `Err` otherwise.
    ///   The `Ok` variant contains the decrypted text, and the `Err` variant contains an error type.
    ///
    /// # Example
    ///
    /// ```
    /// # use ferric_crypto_lib::crypto_systems::ceasar::Ceasar;
    /// # use ferric_crypto_lib::Traits::Decrypt;
    /// let ceasar = Ceasar { shift: 3 };
    /// let result = ceasar.decrypt("KHOOR".into()).unwrap();
    /// assert_eq!(result, "hello".into());
    /// ```
    fn decrypt(&self, cipher_text: BaseString) -> Result<BaseString, CeasarError> {
        let encoded_input = cipher_text.encode()?;

        let decrypted_data: Vec<usize> = encoded_input
            .par_iter()
            .map(|&x| (x + *ALPHABET_LEN - self.shift) % *ALPHABET_LEN)
            .collect();

        let decrypted_input = EncodedString::new(decrypted_data, StringType::Standard);

        let decoded = decrypted_input.decode()?;

        Ok(decoded.to_lowercase())
    }
}

#[cfg(test)]
mod test {
    use crate::crypto_systems::ceasar::Ceasar;
    use crate::error::CeasarError;
    use crate::error::CharacterParseError::InvalidCharacter;
    use crate::Traits::Decrypt;

    #[test]
    fn decrypts_lowercase() {
        let ceasar = Ceasar { shift: 3 };
        let result = ceasar.decrypt("khoor".into()).unwrap();
        assert_eq!(result, "hello".into());
    }

    #[test]
    fn decrypts_uppercase() {
        let ceasar = Ceasar { shift: 3 };
        let result = ceasar.decrypt("KHOOR".into()).unwrap();
        assert_eq!(result, "hello".into());
    }

    #[test]
    fn decrypts_wrap() {
        let ceasar = Ceasar { shift: 4 };
        let result = ceasar.decrypt("ASS".into()).unwrap();
        assert_eq!(result, "zoo".into());
    }

    // Test with invalid data
    #[test]
    fn test_encrypt_digit() {
        let cipher_text = "1234@#!".to_string();

        let ceasar = Ceasar::new_with_rand_shift();

        let result = ceasar.decrypt(cipher_text.into()).unwrap_err();

        match result {
            CeasarError::CharacterParseError(_) => {}
            _ => panic!("Should be Parse error"),
        }
    }

    #[test]
    fn test_decrypt() {
        let text = "IPGCÅCYVCPGÖRHIRU".to_string();

        let ceasar = Ceasar::new(17);

        let result = ceasar.decrypt(text.into()).unwrap();

        assert_eq!(result, "tärningenärkastad".into())
    }
}