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
pub mod base64;
pub mod vigenere;
pub mod xor;

use crate::CipherResult;

pub use base64::b64;
pub use vigenere::vigenere;
pub use xor::xor;

#[derive(Debug, Clone, PartialEq)]
pub struct MethodArgs<'a> {
    pub word: &'a str,
    pub password: &'a str,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Methods<'a> {
    Vigenere(MethodArgs<'a>),
    B64(MethodArgs<'a>),
    Xor(MethodArgs<'a>),
}

pub fn gen_pass(method: &Methods, repeat: Option<u8>) -> CipherResult {
    let encryption_layers = match repeat {
        Some(i) => {
            if i < 1 {
                1
            } else {
                i
            }
        }
        None => 1,
    };
    let mut result: CipherResult = Ok(String::new());

    for _ in 0..encryption_layers {
        result = match method {
            Methods::Vigenere(args) => {
                if result == Ok(String::new()) {
                    result = Ok(args.word.to_owned());
                }
                vigenere(result.unwrap().as_str(), args.password)
            }
            Methods::B64(args) => {
                if result == Ok(String::new()) {
                    result = Ok(args.password.to_owned());
                }
                b64(result.unwrap().as_str())
            }
            Methods::Xor(args) => {
                if result == Ok(String::new()) {
                    result = Ok(args.word.to_owned());
                }
                xor(result.unwrap().as_str(), args.password)
            }
        }
    }

    result
}