ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use crate::Traits::{BruteForce, Decrypt, Encrypt};
#[cfg(feature = "python-integration")]
use pyo3::pyclass;

#[derive(Default, Clone)]
#[cfg_attr(feature = "python-integration", pyclass)]
pub struct Affine {
    pub(crate) a: usize,
    pub(crate) b: usize,
}

// common methods between both python and rust
impl Affine {
    pub fn new(a: usize, b: usize) -> Self {
        Self { a, b }
    }
}

#[cfg(feature = "python-integration")]
mod python_integration {
    use super::*;
    use crate::utils::python_integration::PyBaseString;
    use crate::utils::{BaseString, EncodedString, StringType};
    use gmp_mpfr_sys::mpfr::inp_str;
    use pyo3::prelude::*;
    use std::collections::HashMap;

    #[pymethods]
    impl Affine {
        #[new]
        fn __new__(a: usize, b: usize) -> Self {
            Self::new(a, b)
        }

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

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

        /*pub 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))),
            }
        }*/

        pub fn __str__(&self) -> PyResult<String> {
            let mut s = String::new();
            s.push_str(&format!("Affine Cipher\n"));
            Ok(s)
        }
    }
}