1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

use std::collections::HashMap;

#[derive(Debug)]
#[derive(PartialEq)]
#[derive(Clone)]
pub enum SymetricMethod {
    Vigenere, B64, Xor, Enigma,
}

impl SymetricMethod {

    pub fn get_methods() -> HashMap<String, SymetricMethod> {
        let mut methods: HashMap<String, SymetricMethod> = HashMap::new();

        methods.insert(String::from("Vigenere"), SymetricMethod::Vigenere);
        methods.insert(String::from("Base64"), SymetricMethod::B64);
        methods.insert(String::from("Xor"), SymetricMethod::Xor);
        methods.insert(String::from("Enigma"), SymetricMethod::Enigma);

        methods
    }

    pub fn vigenere(uw: &String, vw: &String) -> String{
        let alphabet = "abcdefghijklmnopqrstuvwxyz";

        let unique: String = uw.to_lowercase();
        let variable: String = vw.to_lowercase();
        let mut new_pass = String::new();

        let mut x = 0;
        for i in unique.chars() {
            let char_position = alphabet.find(i);
            new_pass.push(
                match char_position {
                    Some(mut position) => {
                        position += alphabet.find(variable.as_bytes()[{
                            if x > variable.len() - 1 {
                                x - (variable.len() * (x/variable.len() as usize))
                            } else {
                                x
                            }
                        }] as char).unwrap();
                        position %= alphabet.len();

                        x += 1;
                        alphabet.as_bytes()[position] as char
                    },
                    None => i,
                }
            );
        }

        new_pass

    }

    pub fn b64(vw: &String) -> String {
        let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        let mut binary_word = "".to_string();
        let mut new_pass = String::new();

        for i in vw.clone().into_bytes() {
            binary_word += &format!("0{:b}", i);
        }
        
        let mut padding = "".to_string();
        while binary_word.len() % 6 != 0 {
            binary_word += "00";
            padding += "=";
        }

        let mut x = 0;
        let mut new_binary = String::new();
        for i in binary_word.chars(){
            if x == 6{ new_binary += " "; x = 0 }
            new_binary += &i.to_string();
            x += 1;
        }
        
        let binary_vec: Vec<&str> = new_binary.split(" ").collect();
        for i in binary_vec{
            let number = usize::from_str_radix(i, 2).unwrap();
            new_pass += &(alphabet.as_bytes()[number] as char).to_string();
        }

        new_pass += &padding;
        new_pass
    }

    pub fn xor(uw: &String, vw: &String) -> String {
        String::from(format!("{}, {}", uw, vw))
    }

    pub fn enigma(uw: &String, vw: &String) -> String {
        String::from(format!("{}, {}", uw, vw))
    }

    pub fn gen_pass(method: &Self, uw: &String, vw: &String) -> String {
        match method {
            Self::Vigenere => Self::vigenere(&uw, &vw),
            Self::B64 => Self::b64(&uw),
            Self::Xor => Self::xor(&uw, &vw),
            Self::Enigma => Self::enigma(&uw, &vw),
        }
    }

}

#[derive(Debug)]
#[derive(PartialEq)]
#[derive(Clone)]
pub struct LoginData {
    symetric_method: SymetricMethod,
    suw: String,
    svw: String,
    pub cpw: String
}

impl LoginData {
    pub fn new (method: SymetricMethod, unique_word: String, variable_word: String) -> LoginData {
        LoginData {
            cpw: SymetricMethod::gen_pass(&method, &unique_word, &variable_word),
            symetric_method: method,
            suw: unique_word,
            svw: variable_word,
        }
    }

}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn methods_test() {
        let uw: String = String::from("uniquepass");
        let vw: String = String::from("variablepass");

        assert_eq!(SymetricMethod::vigenere(&uw, &vw), "pnzyufaehs");
        assert_eq!(SymetricMethod::b64(&vw), "dmFyaWFibGVwYXNz");
        assert_eq!(SymetricMethod::xor(&uw, &vw), format!("{}, {}", &uw, &vw));
        assert_eq!(SymetricMethod::enigma(&uw, &vw), format!("{}, {}", &uw, &vw));
    }

    #[test]
    fn gen_test(){
        let method_enum = vec![
            SymetricMethod::Vigenere,
            SymetricMethod::B64,
            SymetricMethod::Xor,
            SymetricMethod::Enigma,
        ];
        let uw: String = String::from("uniquepass");
        let vw: String = String::from("variablepass");
        
        assert_eq!(SymetricMethod::gen_pass(&method_enum[0], &uw, &vw), "pnzyufaehs");
        assert_eq!(SymetricMethod::gen_pass(&method_enum[1], &vw, &uw), "dmFyaWFibGVwYXNz");
        assert_eq!(SymetricMethod::gen_pass(&method_enum[2], &uw, &vw), format!("{}, {}", &uw, &vw));
        assert_eq!(SymetricMethod::gen_pass(&method_enum[3], &uw, &vw), format!("{}, {}", &uw, &vw));
    }

    #[test]
    fn hashmap_test() {
        let methods: HashMap<String, SymetricMethod> = SymetricMethod::get_methods();
        assert_eq!(Some(&SymetricMethod::Vigenere), methods.get("Vigenere"));
        assert_eq!(Some(&SymetricMethod::B64), methods.get("Base64"));
        assert_eq!(Some(&SymetricMethod::Enigma), methods.get("Enigma"));
        assert_eq!(Some(&SymetricMethod::Xor), methods.get("Xor"));
    }

    #[test]
    fn login_test(){
        let method_enum = vec![
            SymetricMethod::Vigenere,
            SymetricMethod::B64,
            SymetricMethod::Xor,
            SymetricMethod::Enigma,
        ];
        let uw: String = String::from("uniquepass");
        let vw: String = String::from("variablepass");

        assert_eq!(LoginData::new(method_enum[0].clone(), "longer".to_string(), "short".to_string()), LoginData {
            symetric_method: method_enum[0].clone(),
            suw: "longer".to_string(),
            svw: "short".to_string(),
            cpw: "dvbxxj".to_string()
        });
        assert_eq!(LoginData::new(method_enum[0].clone(), uw.clone(), vw.clone()), LoginData {
            symetric_method: method_enum[0].clone(),
            suw: uw.clone(),
            svw: vw.clone(),
            cpw: "pnzyufaehs".to_string()
        });
        assert_eq!(LoginData::new(method_enum[1].clone(), vw.clone(), "".to_string()), LoginData {
            symetric_method: method_enum[1].clone(),
            suw: vw.clone(),
            svw: "".to_string(),
            cpw: "dmFyaWFibGVwYXNz".to_string()
        });
        assert_eq!(LoginData::new(method_enum[2].clone(), uw.clone(), vw.clone()), LoginData {
            symetric_method: method_enum[2].clone(),
            suw: uw.clone(),
            svw: vw.clone(),
            cpw: format!("{}, {}", &uw, &vw)
        });
        assert_eq!(LoginData::new(method_enum[3].clone(), uw.clone(), vw.clone()), LoginData {
            symetric_method: method_enum[3].clone(),
            suw: uw.clone(),
            svw: vw.clone(),
            cpw: format!("{}, {}", &uw, &vw)
        });
    }
}