aboutsummaryrefslogtreecommitdiff
path: root/tests/password_generation.rs
blob: a8618a2433bea50a655c67829c7135e79cb1a6ca (plain) (blame)
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
use passwordmaker_rs::{PasswordMaker, Hasher, HasherList, HashAlgorithm, LeetLevel};
use digest::Digest;
use md4;
use md5;
use sha1;
use sha2;
use ripemd;

struct Md4;
struct Md5;
struct Sha1;
struct Sha256;
struct RipeMD160;
impl Hasher for Md4{
    type Output = [u8;16];
    fn hash(data : &[u8]) -> Self::Output {
        md4::Md4::digest(data).into()
    }
}
impl Hasher for Md5{
    type Output = [u8;16];
    fn hash(data : &[u8]) -> Self::Output {
        md5::Md5::digest(data).into()
    }
}
impl Hasher for Sha1{
    type Output = [u8;20];
    fn hash(data : &[u8]) -> Self::Output {
        sha1::Sha1::digest(data).into()
    }
}
impl Hasher for Sha256{
    type Output = [u8;32];
    fn hash(data : &[u8]) -> Self::Output {
        sha2::Sha256::digest(data).into()
    }
}
impl Hasher for RipeMD160{
    type Output = [u8;20];
    fn hash(data : &[u8]) -> Self::Output {
        ripemd::Ripemd160::digest(data).into()
    }
}

impl passwordmaker_rs::Md4 for Md4{}
impl passwordmaker_rs::Md5 for Md5{}
impl passwordmaker_rs::Sha1 for Sha1{}
impl passwordmaker_rs::Sha256 for Sha256{}
impl passwordmaker_rs::Ripemd160 for RipeMD160{}

struct Hashes{}
impl HasherList for Hashes {
    type MD4 = Md4;
    type MD5 = Md5;
    type SHA1 = Sha1;
    type SHA256 = Sha256;
    type RIPEMD160 = RipeMD160;
}

type Pwm<'a> = PasswordMaker<'a, Hashes>;

#[test]
fn default_settings() {
    let pwm = Pwm::new(
        HashAlgorithm::Md5, 
        passwordmaker_rs::UseLeetWhenGenerating::NotAtAll,
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()_-+={}|[]\\:\";'<>?,./",
        "",
        "",
        8,
        "",
        ""
    ).unwrap();
    let result = pwm.generate(".abcdefghij".to_owned(), "1".to_owned()).unwrap();
    assert_eq!(result, "J3>'1F\"/");
}

#[test]
fn v06_compatibility_leading_zeros() {
    let pwm = Pwm::new(
        HashAlgorithm::Md5Version06, 
        passwordmaker_rs::UseLeetWhenGenerating::NotAtAll,
        "whatevr",
        "",
        "",
        8,
        "",
        ""
    ).unwrap();
    let result = pwm.generate("01".to_owned(), "a".to_owned()).unwrap();
    assert_eq!(result, "00d2a735");
}

#[test]
fn regular_md5_no_leading_zeros() {
    let pwm = Pwm::new(
        HashAlgorithm::Md5, 
        passwordmaker_rs::UseLeetWhenGenerating::NotAtAll,
        "0123456789abcdef",
        "",
        "",
        8,
        "",
        ""
    ).unwrap();
    let result = pwm.generate("01".to_owned(), "a".to_owned()).unwrap();
    assert_eq!(result, "d2a73551");
}

/// Tests compatibility with an edge case in the l33t generation of PasswordMaker Pro JavaScript Edition: Word-Final-Sigma.
#[test]
fn word_final_sigma_post_leet() {
    let pwm = Pwm::new(
        HashAlgorithm::Md4, 
        passwordmaker_rs::UseLeetWhenGenerating::After { level: LeetLevel::One },
        "ΣΔΠΖ",
        "",
        "",
        64,
        "",
        ""
    ).unwrap();
    let result = pwm.generate("123456".to_owned(), "password".to_owned()).unwrap();
    assert_eq!(result, "ζδζσσπσζδδσδπζδδδπσπζπζδδζζππσζσσζδπδσζπζππδσπσζζπσζσδπζσζπδσςπδ"); //mind the lunate sigma at character position 61.
}

#[test]
fn hmac_with_upper_bytes() {
    let pwm = Pwm::new(
        HashAlgorithm::HmacRipemd160, 
        passwordmaker_rs::UseLeetWhenGenerating::NotAtAll,
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()_-+={}|[]\\:\";'<>?,./",
        "",
        "",
        41,
        "",
        ""
    ).unwrap();
    let result = pwm.generate("€äß".to_owned(), "password".to_owned()).unwrap();
    assert_eq!(result, "CX'!aI7J+\\.x?:ua'vtaj~c_PBbfATer1tstX_n<}");
}

#[test]
fn v06_yeet_bytes() {
    let pwm = Pwm::new(
        HashAlgorithm::Md5Version06, 
        passwordmaker_rs::UseLeetWhenGenerating::NotAtAll,
        "notused",
        "",
        "",
        47,
        "",
        ""
    ).unwrap();
    let result = pwm.generate("€äß".to_owned(), "password".to_owned()).unwrap();
    assert_eq!(result, "ea552be82dc75c12e6e9d9f30e643e63eeba34536077ce3");
}

#[test]
fn v06_yeet_bytes_hmac() {
    let pwm = Pwm::new(
        HashAlgorithm::HmacMd5Version06, 
        passwordmaker_rs::UseLeetWhenGenerating::NotAtAll,
        "notused",
        "",
        "",
        47,
        "",
        ""
    ).unwrap();
    let result = pwm.generate("€äß".to_owned(), "password".to_owned()).unwrap();
    assert_eq!(result, "28e1392052364d34c7e42e2711ccdd62c67a0a30dbf568a");
}