ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
use crate::crypto_systems::rsa::RSA;
use crate::error::RSAError;
use crate::prelude::*;
use crate::Traits::Encrypt;
use std::str::FromStr;
//use num_bigint::BigUint;
use crate::utils::BaseString;
use rug::Integer;

impl Encrypt<RSAError, BaseString, String> for RSA {
    /// Encrypts a string using the RSA cipher.
    ///
    /// TODO: make decscription
    /// description goes here
    ///
    /// # Arguments
    ///
    /// * `cipher_text` - A `String` that holds the text to be encrypted.
    ///
    /// # Returns
    ///
    /// * A `Result<String, RSAError>` which is `Ok` if the encryption is successful, and `Err` otherwise.
    ///   The `Ok` variant contains the encrypted text, and the `Err` variant contains an error type.
    ///
    /// # Example
    ///
    /// TODO: make example
    fn encrypt(&self, input: BaseString) -> Result<String, RSAError> {
        let input = input.encode_asym()?.flatten();

        let text_num = Integer::from_str(&input).unwrap();

        // encrypt the number
        let encrypted_num = text_num.secure_pow_mod(&self.e, &self.n);

        Ok(encrypted_num.to_string())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Traits::Encrypt;

    // TODO: make tests for RSA encrypt implementation
    #[test]
    fn test_rsa_encrypt() {
        let rsa = RSA::from_public(Integer::from(14317u64), Integer::from(7777u64));
        let cipher_text = "ko".to_string();
        let encrypted_text = rsa.encrypt(cipher_text.into()).unwrap();
        assert_eq!(encrypted_text, "10169");
    }
}