ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use crate::crypto_systems::affine::Affine;
use crate::error::AffineError;
use crate::prelude::ALPHABET_LEN;
use crate::utils::{decode_digit, encode_char, BaseString, EncodedString, StringType};
use crate::Traits::Encrypt;
use rayon::prelude::*;

impl Encrypt<AffineError, BaseString, BaseString> for Affine {
    fn encrypt(&self, input: BaseString) -> Result<BaseString, AffineError> {
        let encoded = input.encode()?;

        // cant be bothered to make it smart and not parrallelize if its not needed
        let encrypted_data: Vec<usize> = encoded
            .par_iter()
            .map(|&x| (x * self.a + self.b) % *ALPHABET_LEN)
            .collect();

        let encrypted_input = EncodedString::new(encrypted_data, StringType::Standard);

        Ok(encrypted_input.decode()?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crypto_systems::affine::Affine;
    use crate::error::AffineError;

    #[test]
    fn encrypts_input_correctly() {
        let affine = Affine::new(5, 8);
        let result = affine.encrypt("hello".into());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "pahhx".into()); // replace with expected encrypted string
    }

    #[test]
    fn encrypts_empty_string() {
        let affine = Affine::new(5, 8);
        let result = affine.encrypt("".into());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "".into()); // assuming encryption of empty string is empty string
    }

    #[test]
    fn encrypts_single_character() {
        let affine = Affine::new(5, 8);
        let result = affine.encrypt("a".into());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "i".into()); // replace with expected encrypted char
    }
}