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
use crate::encrypt::{MethodArgs, Methods};
use std::collections::HashMap;

pub mod encrypt;
pub mod login_data;

#[derive(Debug, Clone, PartialEq)]
pub enum CipherError {
    InvalidCharacterError,
    InvalidMethodError,
}

pub type CipherResult = Result<String, CipherError>;

#[derive(Debug, Clone, PartialEq)]
pub struct LoginData<'a> {
    symetric_method: Methods<'a>,
    pub cpw: CipherResult,
}

pub fn get_methods<'a>() -> HashMap<String, fn(MethodArgs<'a>) -> Methods<'a>> {
    let mut methods: HashMap<String, fn(MethodArgs<'a>) -> Methods<'a>> = HashMap::new();

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

    methods
}

#[cfg(test)]
mod tests {

    use super::*;

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

        assert_eq!(encrypt::vigenere(&uw, &vw), Ok("pnzyufaehs".to_string()));
        assert_eq!(encrypt::b64(&vw), Ok("dmFyaWFibGVwYXNz".to_string()));
        assert_eq!(encrypt::xor(&uw, &vw), Ok("dpyuheds".to_string()));
    }

    #[test]
    fn gen_for_methods_test() {
        let data = encrypt::MethodArgs {
            word: "uniquepass",
            password: "variablepass",
        };
        let vige_method = Methods::Vigenere(data.clone());
        let base_method = Methods::B64(data.clone());
        //let xor_method = Methods::Xor(data.clone());

        let mut methods_vec: Vec<&Methods> = Vec::new();
        let mut methods_vec2: Vec<&Methods> = Vec::new();
        methods_vec.push(&base_method);
        methods_vec.push(&vige_method);

        methods_vec2.push(&vige_method);
        methods_vec2.push(&base_method);

        let test = encrypt::gen_pass_for_methods(methods_vec, vec![None, None]);
        let test2 = encrypt::gen_pass_for_methods(methods_vec2, vec![None, None]);
        assert_eq!(test, Ok("ymwgaxqmqgnotxeh".to_string()));
        assert_eq!(test2, Ok("cG56eXVmYWVocw==".to_string()));
    }

    #[test]
    fn gen_test() {
        let data = encrypt::MethodArgs {
            word: "uniquepass",
            password: "variablepass",
        };
        let vige_method = Methods::Vigenere(data.clone());
        let base_method = Methods::B64(data.clone());
        let xor_method = Methods::Xor(data.clone());

        assert_eq!(
            encrypt::gen_pass(&vige_method, None),
            Ok("pnzyufaehs".to_string())
        );
        assert_eq!(
            encrypt::gen_pass(&base_method, None),
            Ok("dmFyaWFibGVwYXNz".to_string())
        );
        assert_eq!(
            encrypt::gen_pass(&xor_method, None),
            Ok("dpyuheds".to_string())
        );

        assert_eq!(
            encrypt::gen_pass(&vige_method, Some(2)),
            Ok("knqgugliws".to_string())
        );
        assert_eq!(
            encrypt::gen_pass(&base_method, Some(2)),
            Ok("ZG1GeWFXRmliR1Z3WVhOeg==".to_string())
        );
        assert_eq!(
            encrypt::gen_pass(&xor_method, Some(2)),
            Ok("srljhiw".to_string())
        );
    }

    #[test]
    fn hashmap_test<'a>() {
        let methods: HashMap<String, fn(MethodArgs<'a>) -> Methods<'a>> = crate::get_methods();

        for (key, _) in methods.iter() {
            let get_func = methods.get(key).unwrap();
            let _method: Methods = get_func(MethodArgs {
                word: "a",
                password: "b",
            });
        }
    }
}