| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | const K: [u32; 64] = [ |
| 9 | 0xd76a_a478, |
| 10 | 0xe8c7_b756, |
| 11 | 0x2420_70db, |
| 12 | 0xc1bd_ceee, |
| 13 | 0xf57c_0faf, |
| 14 | 0x4787_c62a, |
| 15 | 0xa830_4613, |
| 16 | 0xfd46_9501, |
| 17 | 0x6980_98d8, |
| 18 | 0x8b44_f7af, |
| 19 | 0xffff_5bb1, |
| 20 | 0x895c_d7be, |
| 21 | 0x6b90_1122, |
| 22 | 0xfd98_7193, |
| 23 | 0xa679_438e, |
| 24 | 0x49b4_0821, |
| 25 | 0xf61e_2562, |
| 26 | 0xc040_b340, |
| 27 | 0x265e_5a51, |
| 28 | 0xe9b6_c7aa, |
| 29 | 0xd62f_105d, |
| 30 | 0x0244_1453, |
| 31 | 0xd8a1_e681, |
| 32 | 0xe7d3_fbc8, |
| 33 | 0x21e1_cde6, |
| 34 | 0xc337_07d6, |
| 35 | 0xf4d5_0d87, |
| 36 | 0x455a_14ed, |
| 37 | 0xa9e3_e905, |
| 38 | 0xfcef_a3f8, |
| 39 | 0x676f_02d9, |
| 40 | 0x8d2a_4c8a, |
| 41 | 0xfffa_3942, |
| 42 | 0x8771_f681, |
| 43 | 0x6d9d_6122, |
| 44 | 0xfde5_380c, |
| 45 | 0xa4be_ea44, |
| 46 | 0x4bde_cfa9, |
| 47 | 0xf6bb_4b60, |
| 48 | 0xbebf_bc70, |
| 49 | 0x289b_7ec6, |
| 50 | 0xeaa1_27fa, |
| 51 | 0xd4ef_3085, |
| 52 | 0x0488_1d05, |
| 53 | 0xd9d4_d039, |
| 54 | 0xe6db_99e5, |
| 55 | 0x1fa2_7cf8, |
| 56 | 0xc4ac_5665, |
| 57 | 0xf429_2244, |
| 58 | 0x432a_ff97, |
| 59 | 0xab94_23a7, |
| 60 | 0xfc93_a039, |
| 61 | 0x655b_59c3, |
| 62 | 0x8f0c_cc92, |
| 63 | 0xffef_f47d, |
| 64 | 0x8584_5dd1, |
| 65 | 0x6fa8_7e4f, |
| 66 | 0xfe2c_e6e0, |
| 67 | 0xa301_4314, |
| 68 | 0x4e08_11a1, |
| 69 | 0xf753_7e82, |
| 70 | 0xbd3a_f235, |
| 71 | 0x2ad7_d2bb, |
| 72 | 0xeb86_d391, |
| 73 | ]; |
| 74 | |
| 75 | |
| 76 | const S: [u32; 64] = [ |
| 77 | 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, |
| 78 | 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, |
| 79 | 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, |
| 80 | ]; |
| 81 | |
| 82 | |
| 83 | pub struct Md5 { |
| 84 | state: [u32; 4], |
| 85 | buffer: [u8; 64], |
| 86 | buffered: usize, |
| 87 | length: u64, |
| 88 | } |
| 89 | |
| 90 | impl Md5 { |
| 91 | |
| 92 | pub fn new() -> Md5 { |
| 93 | Md5 { |
| 94 | state: [0x6745_2301, 0xefcd_ab89, 0x98ba_dcfe, 0x1032_5476], |
| 95 | buffer: [0; 64], |
| 96 | buffered: 0, |
| 97 | length: 0, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | pub fn update(&mut self, mut data: &[u8]) { |
| 103 | self.length = self.length.wrapping_add(data.len() as u64); |
| 104 | if self.buffered > 0 { |
| 105 | let take = (64 - self.buffered).min(data.len()); |
| 106 | self.buffer[self.buffered..self.buffered + take].copy_from_slice(&data[..take]); |
| 107 | self.buffered += take; |
| 108 | data = &data[take..]; |
| 109 | if self.buffered == 64 { |
| 110 | let block = self.buffer; |
| 111 | self.compress(&block); |
| 112 | self.buffered = 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | if data.is_empty() { |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #[allow(clippy::chunks_exact_to_as_chunks)] |
| 122 | let mut chunks = data.chunks_exact(64); |
| 123 | for block in &mut chunks { |
| 124 | let mut b = [0u8; 64]; |
| 125 | b.copy_from_slice(block); |
| 126 | self.compress(&b); |
| 127 | } |
| 128 | let rest = chunks.remainder(); |
| 129 | self.buffer[..rest.len()].copy_from_slice(rest); |
| 130 | self.buffered = rest.len(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | pub fn finish(mut self) -> [u8; 16] { |
| 135 | let bit_len = self.length.wrapping_mul(8); |
| 136 | self.update(&[0x80]); |
| 137 | |
| 138 | while self.buffered != 56 { |
| 139 | self.update(&[0]); |
| 140 | } |
| 141 | let mut block = self.buffer; |
| 142 | block[56..].copy_from_slice(&bit_len.to_le_bytes()); |
| 143 | self.compress(&block); |
| 144 | let mut out = [0u8; 16]; |
| 145 | for (i, word) in self.state.iter().enumerate() { |
| 146 | out[i * 4..i * 4 + 4].copy_from_slice(&word.to_le_bytes()); |
| 147 | } |
| 148 | out |
| 149 | } |
| 150 | |
| 151 | fn compress(&mut self, block: &[u8; 64]) { |
| 152 | let mut m = [0u32; 16]; |
| 153 | for (i, word) in m.iter_mut().enumerate() { |
| 154 | *word = u32::from_le_bytes([ |
| 155 | block[i * 4], |
| 156 | block[i * 4 + 1], |
| 157 | block[i * 4 + 2], |
| 158 | block[i * 4 + 3], |
| 159 | ]); |
| 160 | } |
| 161 | let [mut a, mut b, mut c, mut d] = self.state; |
| 162 | for i in 0..64 { |
| 163 | let (f, g) = match i / 16 { |
| 164 | 0 => ((b & c) | (!b & d), i), |
| 165 | 1 => ((d & b) | (!d & c), (5 * i + 1) % 16), |
| 166 | 2 => (b ^ c ^ d, (3 * i + 5) % 16), |
| 167 | _ => (c ^ (b | !d), (7 * i) % 16), |
| 168 | }; |
| 169 | let tmp = d; |
| 170 | d = c; |
| 171 | c = b; |
| 172 | b = b.wrapping_add( |
| 173 | a.wrapping_add(f) |
| 174 | .wrapping_add(K[i]) |
| 175 | .wrapping_add(m[g]) |
| 176 | .rotate_left(S[i]), |
| 177 | ); |
| 178 | a = tmp; |
| 179 | } |
| 180 | self.state[0] = self.state[0].wrapping_add(a); |
| 181 | self.state[1] = self.state[1].wrapping_add(b); |
| 182 | self.state[2] = self.state[2].wrapping_add(c); |
| 183 | self.state[3] = self.state[3].wrapping_add(d); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | impl Default for Md5 { |
| 188 | fn default() -> Self { |
| 189 | Md5::new() |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[cfg(test)] |
| 194 | mod tests { |
| 195 | use super::*; |
| 196 | |
| 197 | fn hex(digest: [u8; 16]) -> String { |
| 198 | digest.iter().map(|b| format!("{b:02x}")).collect() |
| 199 | } |
| 200 | |
| 201 | fn md5(data: &[u8]) -> String { |
| 202 | let mut m = Md5::new(); |
| 203 | m.update(data); |
| 204 | hex(m.finish()) |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn rfc1321_test_suite() { |
| 209 | assert_eq!(md5(b""), "d41d8cd98f00b204e9800998ecf8427e"); |
| 210 | assert_eq!(md5(b"a"), "0cc175b9c0f1b6a831c399e269772661"); |
| 211 | assert_eq!(md5(b"abc"), "900150983cd24fb0d6963f7d28e17f72"); |
| 212 | assert_eq!(md5(b"message digest"), "f96b697d7cb7938d525a2f31aaf161d0"); |
| 213 | assert_eq!( |
| 214 | md5(b"abcdefghijklmnopqrstuvwxyz"), |
| 215 | "c3fcd3d76192e4007dfb496cca67e13b" |
| 216 | ); |
| 217 | assert_eq!( |
| 218 | md5(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), |
| 219 | "d174ab98d277d9f5a5611c2c9f419d9f" |
| 220 | ); |
| 221 | assert_eq!( |
| 222 | md5( |
| 223 | b"12345678901234567890123456789012345678901234567890123456789012345678901234567890" |
| 224 | ), |
| 225 | "57edf4a22be3c955ac49da2e2107b67a" |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn chunked_updates_match_one_shot() { |
| 231 | let data: Vec<u8> = (0..1000u32).map(|i| (i * 7) as u8).collect(); |
| 232 | let mut whole = Md5::new(); |
| 233 | whole.update(&data); |
| 234 | let mut split = Md5::new(); |
| 235 | for chunk in data.chunks(7) { |
| 236 | split.update(chunk); |
| 237 | } |
| 238 | assert_eq!(whole.finish(), split.finish()); |
| 239 | } |
| 240 | } |