ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use crate::decrypt::ceasar::*;
use crate::encrypt::ceasar::*;
use crate::error::CharacterParseError;
use crate::prelude::ALPHABET_LEN;
use crate::Traits::{BruteForce, Decrypt, Encrypt};
#[cfg(feature = "python-integration")]
use pyo3::{pyclass, PyErr};
#[cfg(feature = "python-integration")]
use pyo3_helper_macros::py3_bind_pub;
use rand::thread_rng;
use rand::Rng;

/// Represents a Caesar cipher.
///
/// A Caesar cipher is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet.
/// For example, with a shift of 1, A would be replaced by B, B would become C, and so on.
///
/// # Fields
///
/// * `shift` - A usize that represents the shift value used for encryption and decryption.
#[derive(Default, Clone)]
#[cfg_attr(feature = "python-integration", pyclass(get_all))]
pub struct Ceasar {
    pub shift: usize,
}

#[cfg_attr(feature = "python-integration", py3_bind_pub)]
impl Ceasar {
    /// Creates a new Caesar cipher with the given shift value.
    ///
    /// # Arguments
    ///
    /// * `shift` - A usize that represents the shift value.
    ///
    /// # Returns
    ///
    /// * A new Caesar cipher with the specified shift value.
    ///
    /// # Example
    ///
    /// ```
    /// # use ferric_crypto_lib::crypto_systems::ceasar::Ceasar;
    /// let ceasar = Ceasar::new(3);
    /// ```
    pub fn new(shift: usize) -> Self {
        Self { shift }
    }

    /// Creates a new Caesar cipher with a random shift value between 1 and 25.
    ///
    /// # Returns
    ///
    /// * A new Caesar cipher with a random shift value.
    ///
    /// # Example
    ///
    /// ```
    /// # use ferric_crypto_lib::crypto_systems::ceasar::Ceasar;
    ///
    /// let ceasar = Ceasar::new_with_rand_shift();
    /// ```
    pub fn new_with_rand_shift() -> Self {
        // select random shift
        let shift = thread_rng().gen_range(1..*ALPHABET_LEN);
        Self { shift }
    }

    pub fn set_key(&mut self, key: usize) {
        self.shift = key;
    }
}


#[cfg(feature = "python-integration")]
mod python_integration {
    use super::*;
    use crate::utils::python_integration::PyBaseString;
    use crate::utils::BaseString;
    use pyo3::prelude::*;
    use pyo3::{pyclass, pymethods, PyResult};
    use std::collections::HashMap;
    use pyo3::types::PyString;
    use crate::utils::python_integration::StringOrBaseString;

    #[pymethods]
    impl Ceasar {
        fn __str__(&self) -> PyResult<String> {
            Ok(format!("Ceasar Cipher: shift = {}", self.shift))
        }

        fn encrypt(&self, input: StringOrBaseString) -> PyResult<BaseString> {
            match Encrypt::encrypt(self, input.into()) {
                Ok(s) => Ok(s.into()),
                Err(e) => Err(pyo3::exceptions::PyException::new_err(format!("{:?}", e))),
            }
        }

        fn decrypt(&self, input: StringOrBaseString) -> PyResult<BaseString> {
            match Decrypt::decrypt(self, input.into()) {
                Ok(s) => Ok(s.into()),
                Err(e) => Err(pyo3::exceptions::PyException::new_err(format!("{:?}", e))),
            }
        }

        fn brute_force(
            &mut self,
            input: String,
            clear_text: Option<String>,
        ) -> PyResult<HashMap<usize, String>> {
            match BruteForce::brute_force(self, input, clear_text, None) {
                Ok(s) => Ok(s),
                Err(e) => Err(pyo3::exceptions::PyException::new_err(format!("{:?}", e))),
            }
        }
    }
}