Skip to main content

cryptose/
lib.rs

1pub use self::encryption::encrypt;
2pub use self::decryption::decrypt;
3/// Encrypts a string into a string consisting of ASCII u8 integers.
4/// 
5/// # Examples:
6/// 
7/// ```
8/// let arg = String::from("hello");
9/// let encrypted = cryptose::encrypt(&arg);
10/// 
11/// assert_eq!("730173317311173801738017310173401".to_string(), encrypted.trim());
12/// ```
13pub mod encryption {
14    pub fn encrypt(strg: &String) -> String {
15        let mut vec: Vec<char> = Vec::new();
16        for l in strg.chars() {
17            vec.push(l);
18        }
19        let map_enc = vec.iter().map(|e| e.to_ascii_lowercase() as u8);
20        let vec_enc: Vec<u8> = map_enc.clone().collect();
21        let mut str_enc: String = String::new();
22        for e in vec_enc.iter() {
23            str_enc.push_str(e.to_string().as_str());
24            str_enc.push_str("37");
25        }
26        let str_final = str_enc.trim().chars().rev().collect::<String>();
27        str_final
28    }
29}
30/// Decrypts an encrypted - via the cryptose::encrypt() function - string,
31/// consisting of ASCII u8 integers into a string.
32/// 
33/// # Examples:
34/// 
35/// ```
36/// let arg = String::from("730173317310173997350173011");
37/// let decrypted = cryptose::decrypt(&arg);
38/// 
39/// assert_eq!("nice".to_string(), decrypted.trim());
40/// ```
41/// 
42/// # Panics (AVOID doing these!):
43/// 
44/// -- Calling the decrypt() function with a string of non-integer characters as a parameter.
45/// 
46/// ```
47/// let arg_panics = String::from("hello");
48/// let decrypted = cryptose::decrypt(&arg_panics);
49/// ```
50/// 
51/// The above block of code will cause your program to panic.
52pub mod decryption {
53    pub fn decrypt(strg: &String) -> String {
54        let str_enc = strg.trim().chars().rev().collect::<String>();
55        let str1 = str_enc.replace("37", " ");
56        let vec1: Vec<&str> = str1.split_whitespace().collect();
57        let map = vec1.iter().map(|_e| _e.parse::<u8>().unwrap() as char);
58        let vec2: Vec<char> = map.clone().collect::<Vec<char>>();
59        let mut str_dec: String = String::new();
60        for e in vec2.iter() {
61            str_dec.push_str(e.to_string().as_str());
62        }
63        str_dec
64    }
65}