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
#[macro_use]
extern crate lazy_static;
lazy_static!{}
pub mod prelude;
#[cfg(test)]
mod tests {
extern crate md5;
extern crate rand;
use prelude::{Hasher, Reducer, RainbowTable};
use self::rand::{thread_rng, Rng};
struct MD5Hasher;
impl Hasher for MD5Hasher {
fn digest(&self, plaintext : &str) -> String {
format!("{:x}", md5::compute(plaintext.as_bytes()))
}
}
struct SubstringReducer {
n: usize
}
impl Reducer for SubstringReducer {
fn reduce(&self, hash : &str) -> String {
String::from(&hash[..self.n])
}
}
lazy_static! {
static ref TABLE : RainbowTable<MD5Hasher, SubstringReducer> = {
let mut rfs : Vec<SubstringReducer> = Vec::new();
for i in 6..27 {
rfs.push(SubstringReducer { n : i });
}
let mut table : RainbowTable<MD5Hasher, SubstringReducer> = RainbowTable::new(MD5Hasher, rfs);
for i in 0..1000 {
table.add_seed(format!("{}", i));
}
table
};
}
#[test]
fn test_rainbow_table_find_seeds() {
for i in 0..1000 {
let seed = format!("{}", i);
let seed_hash = format!("{:x}", md5::compute(seed.as_bytes()));
assert_eq!(Some(seed), TABLE.find_plaintext(&seed_hash));
}
}
#[test]
fn test_rainbow_table_find_random() {
let mut rfs : Vec<SubstringReducer> = Vec::new();
let hf = MD5Hasher;
for i in 6..27 {
rfs.push(SubstringReducer { n : i });
}
let mut rng = thread_rng();
for _ in 0..1000 {
let mut value = format!("{}", rng.gen_range(0, 1000));
let reductions = rng.gen_range(1, rfs.len());
for i in 0..reductions {
value = rfs[i].reduce(&hf.digest(&value[..])[..]);
}
let mut hash = hf.digest(&value[..]);
assert_eq!(Some(value), TABLE.find_plaintext(&hash));
}
}
#[test]
fn test_rainbow_table_find_none() {
assert_eq!(None, TABLE.find_plaintext("8a1fb399dcf220db935995abce6a1532"));
}
}