Mercurial > projects > dcrypt
comparison dcrypt/crypto/hashes/MD5.d @ 28:ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sun, 10 May 2009 22:38:48 -0400 |
parents | 8b5eaf3c2979 |
children |
comparison
equal
deleted
inserted
replaced
27:8b5eaf3c2979 | 28:ad687db713a4 |
---|---|
17 * References: http://www.faqs.org/rfcs/rfc1321.html | 17 * References: http://www.faqs.org/rfcs/rfc1321.html |
18 * Bugs: MD5 is not cryptographically secure. | 18 * Bugs: MD5 is not cryptographically secure. |
19 */ | 19 */ |
20 class MD5 : Hash | 20 class MD5 : Hash |
21 { | 21 { |
22 private uint h0, h1, h2, h3; | 22 private uint h0, h1, h2, h3; |
23 | 23 |
24 // Shift amounts | 24 // Shift amounts |
25 private enum | 25 private enum |
26 { | 26 { |
27 S11 = 7, | 27 S11 = 7, |
59 uint digestSize() | 59 uint digestSize() |
60 { | 60 { |
61 return 16; | 61 return 16; |
62 } | 62 } |
63 | 63 |
64 char[] name() | 64 string name() |
65 { | 65 { |
66 return "MD5"; | 66 return "MD5"; |
67 } | 67 } |
68 | 68 |
69 void transform(ubyte[] input) | 69 void transform(ubyte[] input) |
206 a += b; | 206 a += b; |
207 } | 207 } |
208 | 208 |
209 ubyte[] digest() | 209 ubyte[] digest() |
210 { | 210 { |
211 padMessage(MODE_MD); | 211 padMessage(MODE_MD); |
212 ubyte[] result = new ubyte[digestSize]; | 212 ubyte[] result = new ubyte[digestSize]; |
213 | 213 |
214 result[0..4] = ByteConverter.LittleEndian.from!(uint)(h0); | 214 result[0..4] = ByteConverter.LittleEndian.from!(uint)(h0); |
215 result[4..8] = ByteConverter.LittleEndian.from!(uint)(h1); | 215 result[4..8] = ByteConverter.LittleEndian.from!(uint)(h1); |
216 result[8..12] = ByteConverter.LittleEndian.from!(uint)(h2); | 216 result[8..12] = ByteConverter.LittleEndian.from!(uint)(h2); |
243 debug (UnitTest) | 243 debug (UnitTest) |
244 { | 244 { |
245 // Found in Tango <3 | 245 // Found in Tango <3 |
246 unittest | 246 unittest |
247 { | 247 { |
248 static const char[][] test_inputs = [ | 248 static string[] test_inputs = [ |
249 "", | 249 "", |
250 "a", | 250 "a", |
251 "abc", | 251 "abc", |
252 "message digest", | 252 "message digest", |
253 "abcdefghijklmnopqrstuvwxyz", | 253 "abcdefghijklmnopqrstuvwxyz", |
254 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | 254 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
255 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" | 255 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" |
256 ]; | 256 ]; |
257 | 257 |
258 static const char[][] test_results = [ | 258 static string[] test_results = [ |
259 "d41d8cd98f00b204e9800998ecf8427e", | 259 "d41d8cd98f00b204e9800998ecf8427e", |
260 "0cc175b9c0f1b6a831c399e269772661", | 260 "0cc175b9c0f1b6a831c399e269772661", |
261 "900150983cd24fb0d6963f7d28e17f72", | 261 "900150983cd24fb0d6963f7d28e17f72", |
262 "f96b697d7cb7938d525a2f31aaf161d0", | 262 "f96b697d7cb7938d525a2f31aaf161d0", |
263 "c3fcd3d76192e4007dfb496cca67e13b", | 263 "c3fcd3d76192e4007dfb496cca67e13b", |
264 "d174ab98d277d9f5a5611c2c9f419d9f", | 264 "d174ab98d277d9f5a5611c2c9f419d9f", |
265 "57edf4a22be3c955ac49da2e2107b67a" | 265 "57edf4a22be3c955ac49da2e2107b67a" |
266 ]; | 266 ]; |
267 | 267 |
268 MD5 h = new MD5(); | 268 MD5 h = new MD5(); |
269 foreach (uint i, char[] input; test_inputs) | 269 foreach (uint i, string input; test_inputs) |
270 { | 270 { |
271 h.update(input); | 271 h.update(input); |
272 char[] digest = h.hexDigest(); | 272 string digest = h.hexDigest(); |
273 assert(digest == test_results[i], | 273 assert(digest == test_results[i], |
274 h.name~": ("~digest~") != ("~test_results[i]~")"); | 274 h.name~": ("~digest~") != ("~test_results[i]~")"); |
275 } | 275 } |
276 } | 276 } |
277 } | 277 } |