]>
Commit | Line | Data |
---|---|---|
eca0802b KS |
1 | use std::fmt; |
2 | ||
3 | const MD5_SHIFTS: [u8; 64] = [ | |
4 | 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, | |
5 | 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, | |
6 | 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, | |
7 | 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 | |
8 | ]; | |
9 | ||
10 | const MD5_K: [u32; 64] = [ | |
11 | 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, | |
12 | 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821, | |
13 | 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8, | |
14 | 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A, | |
15 | 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70, | |
16 | 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665, | |
17 | 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1, | |
18 | 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391 | |
19 | ]; | |
20 | ||
21 | const INITIAL_MD5_HASH: [u32; 4] = [ 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 ]; | |
22 | ||
23 | fn box0(b: u32, c: u32, d: u32) -> u32 { (b & c) | (!b & d) } | |
24 | fn box1(b: u32, c: u32, d: u32) -> u32 { (d & b) | (!d & c) } | |
25 | fn box2(b: u32, c: u32, d: u32) -> u32 { b ^ c ^ d } | |
26 | fn box3(b: u32, c: u32, d: u32) -> u32 { c ^ (b | !d) } | |
27 | ||
28 | #[derive(Clone)] | |
29 | pub struct MD5 { | |
30 | pub hash: [u32; 4], | |
31 | inwords: [u32; 16], | |
32 | buf: [u8; 64], | |
33 | pos: usize, | |
34 | count: usize, | |
35 | } | |
36 | ||
37 | impl PartialEq for MD5 { | |
38 | fn eq(&self, other: &Self) -> bool { | |
39 | self.hash == other.hash | |
40 | } | |
41 | } | |
42 | ||
43 | impl Eq for MD5 { } | |
44 | ||
45 | impl fmt::Display for MD5 { | |
46 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
47 | write!(f, "{:08x}{:08x}{:08x}{:08x}", self.hash[0].swap_bytes(), self.hash[1].swap_bytes(), self.hash[2].swap_bytes(), self.hash[3].swap_bytes()) | |
48 | } | |
49 | } | |
50 | ||
51 | macro_rules! round { | |
52 | ($a: ident, $b: ident, $c: ident, $d: ident, $box: ident, $k: expr, $inval: expr) => { | |
53 | let f = $box($b, $c, $d).wrapping_add($a).wrapping_add(MD5_K[$k]).wrapping_add($inval); | |
54 | $a = $d; | |
55 | $d = $c; | |
56 | $c = $b; | |
57 | $b = $b.wrapping_add(f.rotate_left(MD5_SHIFTS[$k].into())); | |
58 | } | |
59 | } | |
60 | ||
61 | #[allow(dead_code)] | |
62 | impl MD5 { | |
63 | pub fn new() -> Self { | |
64 | Self { | |
65 | hash: INITIAL_MD5_HASH, | |
66 | inwords: [0; 16], | |
67 | buf: [0; 64], | |
68 | pos: 0, | |
69 | count: 0, | |
70 | } | |
71 | } | |
72 | fn calc_one_block(&mut self) { | |
73 | let mut a = self.hash[0]; | |
74 | let mut b = self.hash[1]; | |
75 | let mut c = self.hash[2]; | |
76 | let mut d = self.hash[3]; | |
77 | ||
78 | for (out, src) in self.inwords.iter_mut().zip(self.buf.chunks_exact(4)) { | |
79 | *out = (u32::from(src[0]) << 0) | | |
80 | (u32::from(src[1]) << 8) | | |
81 | (u32::from(src[2]) << 16) | | |
82 | (u32::from(src[3]) << 24); | |
83 | } | |
84 | ||
85 | for k in 0..16 { round!(a, b, c, d, box0, k, self.inwords[k]); } | |
86 | for k in 16..32 { round!(a, b, c, d, box1, k, self.inwords[(5 * k + 1) & 0xF]); } | |
87 | for k in 32..48 { round!(a, b, c, d, box2, k, self.inwords[(3 * k + 5) & 0xF]); } | |
88 | for k in 48..64 { round!(a, b, c, d, box3, k, self.inwords[(7 * k) & 0xF]); } | |
89 | ||
90 | self.hash[0] = self.hash[0].wrapping_add(a); | |
91 | self.hash[1] = self.hash[1].wrapping_add(b); | |
92 | self.hash[2] = self.hash[2].wrapping_add(c); | |
93 | self.hash[3] = self.hash[3].wrapping_add(d); | |
94 | ||
95 | self.pos = 0; | |
96 | } | |
97 | pub fn update_hash(&mut self, src: &[u8]) { | |
98 | for byte in src.iter() { | |
99 | self.buf[self.pos] = *byte; | |
100 | self.pos += 1; | |
101 | if self.pos == 64 { | |
102 | self.calc_one_block(); | |
103 | } | |
104 | } | |
105 | self.count += src.len(); | |
106 | } | |
107 | pub fn finish(&mut self) { | |
108 | self.buf[self.pos] = 0x80; | |
109 | self.pos += 1; | |
110 | if self.pos > 48 { | |
111 | while self.pos < 64 { | |
112 | self.buf[self.pos] = 0x00; | |
113 | self.pos += 1; | |
114 | } | |
115 | self.calc_one_block(); | |
116 | } | |
117 | while self.pos < 64 { | |
118 | self.buf[self.pos] = 0x00; | |
119 | self.pos += 1; | |
120 | } | |
121 | for i in 0..8 { | |
122 | self.buf[56 + i] = ((self.count * 8) >> (i * 8)) as u8; | |
123 | } | |
124 | self.calc_one_block(); | |
125 | } | |
126 | pub fn get_hash(&self, dst: &mut [u32; 4]) { | |
127 | for (dst, src) in dst.iter_mut().zip(self.hash.iter()) { | |
128 | *dst = src.swap_bytes(); | |
129 | } | |
130 | } | |
131 | pub fn get_hash_bytes(&self, dst: &mut [u8; 4]) { | |
132 | for (dst, src) in dst.chunks_exact_mut(4).zip(self.hash.iter()) { | |
133 | dst[0] = (*src >> 0) as u8; | |
134 | dst[1] = (*src >> 8) as u8; | |
135 | dst[2] = (*src >> 16) as u8; | |
136 | dst[3] = (*src >> 24) as u8; | |
137 | } | |
138 | } | |
139 | pub fn calculate_hash(src: &[u8], hash: &mut [u32; 4]) { | |
140 | let mut md5 = Self::new(); | |
141 | md5.update_hash(src); | |
142 | md5.finish(); | |
143 | md5.get_hash(hash); | |
144 | } | |
145 | } | |
146 | ||
147 | #[cfg(test)] | |
148 | mod test { | |
149 | use super::*; | |
150 | ||
151 | #[test] | |
152 | fn test_md5() { | |
153 | let mut hash = [0u32; 4]; | |
154 | ||
155 | MD5::calculate_hash(&[], &mut hash); | |
156 | assert_eq!(hash, [ 0xD41D8CD9, 0x8F00B204, 0xE9800998, 0xECF8427E ]); | |
157 | ||
158 | MD5::calculate_hash(b"abc", &mut hash); | |
159 | assert_eq!(hash, [ 0x90015098, 0x3CD24FB0, 0xD6963F7D, 0x28E17F72 ]); | |
160 | ||
161 | MD5::calculate_hash(b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", &mut hash); | |
162 | assert_eq!(hash, [ 0x8215EF07, 0x96A20BCA, 0xAAE116D3, 0x876C664A ]); | |
163 | ||
164 | let mut md5 = MD5::new(); | |
165 | for _ in 0..1000000 { | |
166 | md5.update_hash(b"a"); | |
167 | } | |
168 | md5.finish(); | |
169 | md5.get_hash(&mut hash); | |
170 | assert_eq!(hash, [ 0x7707D6AE, 0x4E027C70, 0xEEA2A935, 0xC2296F21 ]); | |
171 | } | |
172 | } |