ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use gmp_mpfr_sys::mpfr::ui_sub;
use crate::Traits::{BruteForce, Decrypt, Encrypt};
#[cfg(feature = "python-integration")]
use pyo3::pyclass;
#[cfg(feature = "python-integration")]
use pyo3_helper_macros::py3_bind_pub;
use crate::utils::{cyclic_left_shift, mod_add};

// block of len 8 all other chars are ignored during encryption and decryption

// L0 = (m0, m1, m2, m3), R0 = (m4, m5, m6, m7)
// Li = Ri-1 Ri ≡ Li-1 + f(Ri-1, Ki) (mod Alphabet len)
// i = 1, 2, 3
// c = (L1, R3)
// f (Ri−1 , Ki ) ≡ P2 A(P1 Ri−1 + Ki ) (mod 28)
// K = (k1, k2, k3)
// K0 ≡ (k3 , k1 + k2 , k2 , k2 + k3 , k1 , k1 + k3 ) (mod 28)
// S = (3, 5, 7, 11, 13, 17)
// Ki ≡ LSi+2 (Ki ) + S (mod 28) # där LSj (K) betecknar ett cykliskt vänster skift j steg av elementen i K

#[derive(Default, Debug, Clone)]
#[cfg_attr(feature = "python-integration", pyclass)]
pub struct Vahf {
    main_key: Vec<usize>,
    pub(crate) keys: Vec<Vec<usize>>,
}

// common methods between both python and rust
#[cfg_attr(feature = "python-integration", py3_bind_pub)]
impl Vahf {
    const S: [u32; 6] = [3, 5, 7, 11, 13, 17];

    pub fn new(k1: usize, k2: usize, k3: usize) -> Self {
        let mut vahf = Self {
            main_key: vec![k1, k2, k3],
            keys: Vec::new()
        };
        vahf.generate_keys();
        vahf
    }

    fn generate_keys(&mut self) {
        let shifts = &Self::S[..3]; // The S sequence (maybe idk)
        let k0 = vec![
            self.main_key[2],                         // k3
            mod_add(self.main_key[0], self.main_key[1]),   // k1 + k2
            self.main_key[1],                         // k2
            mod_add(self.main_key[1], self.main_key[2]),   // k2 + k3
            self.main_key[0],                         // k1
            mod_add(self.main_key[0], self.main_key[2]),   // k1 + k3
        ];

        self.keys = shifts.iter().map(|&shift| {
            let shifted = cyclic_left_shift(&k0, shift as usize); // Apply cyclic left shift to k0
            shifted
        }).collect();
    }

    pub fn get_debug_info(&self) -> String {
        let mut s = String::new();
        s.push_str(&"Vahf Cipher\n".to_string());
        s.push_str(&format!("Main Key: {:?}\n", self.main_key));
        s.push_str(&format!("Keys: {:?}\n", self.keys));
        s
    }
}
/*
#[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 Vahf {
        #[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)
        }
    }
}*/