cryptatools_core/utils/
convert.rs

1use crate::utils::alphabets::Alphabet;
2use crate::utils::alphabets::uniffy_opcode_group;
3
4pub struct Decode {
5
6}
7
8impl Decode {
9    ///  Decode a vector of u8 to ascii text string.
10    ///
11    ///  ```
12    ///  use cryptatools_core::utils::convert;
13    ///  let ascii: String = convert::Decode::from_u8_to_ascii(vec![0x41, 0x41, 0x41]);
14    ///  assert_eq!("AAA", ascii);
15    ///  ```
16    /// 
17    ///  ```
18    ///  use cryptatools_core::utils::convert;
19    ///  let ascii: String = convert::Decode::from_u8_to_ascii(vec![0x48, 0x69, 0x21]);
20    ///  assert_eq!("Hi!", ascii);
21    ///  ```
22    pub fn from_u8_to_ascii(u8_input: Vec<u8>) -> String {
23        String::from(std::str::from_utf8(u8_input.as_slice()).unwrap())
24    }
25
26    /*pub fn decode(alphabet: HashMap<String, Vec<u8>>, encoded: Vec<u8>) -> String {
27
28    }*/
29}
30pub struct Encode {
31
32}
33impl Encode {
34    ///  Encode a string to a vector of u8 bytes.
35    ///
36    ///  ```
37    ///  use cryptatools_core::utils::convert;
38    ///  let bytes: Vec<u8> = convert::Encode::from_ascii_to_u8(String::from("AAA"));
39    ///  assert_eq!(vec![0x41, 0x41, 0x41], bytes);
40    ///  ```
41    pub fn from_ascii_to_u8(ascii_input: String) -> Vec<u8> {
42        let mut bytes = vec![];
43        for character in ascii_input.chars() {
44            bytes.push(char::from(character) as u8)
45        }
46
47        bytes
48    }
49
50    /// Encode the input argument `unencoded` to a byte according the `alphabet`.
51    /// 
52    /// ```
53    /// use cryptatools_core::utils::convert;
54    /// use cryptatools_core::utils::alphabets::{Alphabet};
55    /// 
56    /// let ascii_alphabet = Alphabet::new_empty().ascii_encoding();
57    /// let encoded: Vec<u8> = convert::Encode::encode(&ascii_alphabet, String::from("ABCDEFGH"));
58    /// assert_eq!(encoded, vec![65, 66, 67, 68, 69, 70, 71, 72]);
59    /// 
60    /// let pokered_alphabet = Alphabet::new_empty().pokered_charset_encoding();
61    /// let pokered_encoded = convert::Encode::encode(&pokered_alphabet, String::from("<NULL><PAGE><PKMN>"));
62    /// assert_eq!(pokered_encoded, vec![0, 73, 74]);
63    /// ```
64    pub fn encode(alphabet: &Alphabet, unecoded: String) -> Vec<u8> {
65        let mut encoded: Vec<Vec<u8>> = vec![];
66        let mut stack = String::from("");
67
68        for c in unecoded.chars() {
69            let char_str = String::from(c);
70
71            if alphabet.encoding.contains_left(&char_str) {
72                if let Some(encoded_byte) = alphabet.encoding.get_by_left(&char_str) {
73                    encoded.push(encoded_byte.to_vec());
74                }
75                stack = String::from("");
76            } else {
77                stack.push_str(&char_str);
78                if alphabet.encoding.contains_left(&stack) {
79                    if let Some(encoded_byte) = alphabet.encoding.get_by_left(stack.as_str()).clone() {
80                        encoded.push(encoded_byte.clone());
81                    }
82                    stack = String::from("");
83                }
84            }
85        }
86
87        uniffy_opcode_group(encoded)
88    }
89}