Mercurial > projects > dcrypt
comparison dcrypt/crypto/hashes/MD4.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/rfc1320.html | 17 * References: http://www.faqs.org/rfcs/rfc1320.html |
18 * Bugs: MD4 is not cryptographically secure. | 18 * Bugs: MD4 is not cryptographically secure. |
19 */ | 19 */ |
20 class MD4 : Hash | 20 class MD4 : 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 = 3, | 27 S11 = 3, |
54 uint digestSize() | 54 uint digestSize() |
55 { | 55 { |
56 return 16; | 56 return 16; |
57 } | 57 } |
58 | 58 |
59 char[] name() | 59 string name() |
60 { | 60 { |
61 return "MD4"; | 61 return "MD4"; |
62 } | 62 } |
63 | 63 |
64 void transform(ubyte[] input) | 64 void transform(ubyte[] input) |
166 a = Bitwise.rotateLeft(a, s); | 166 a = Bitwise.rotateLeft(a, s); |
167 } | 167 } |
168 | 168 |
169 ubyte[] digest() | 169 ubyte[] digest() |
170 { | 170 { |
171 padMessage(MODE_MD); | 171 padMessage(MODE_MD); |
172 ubyte[] result = new ubyte[digestSize]; | 172 ubyte[] result = new ubyte[digestSize]; |
173 | 173 |
174 result[0..4] = ByteConverter.LittleEndian.from!(uint)(h0); | 174 result[0..4] = ByteConverter.LittleEndian.from!(uint)(h0); |
175 result[4..8] = ByteConverter.LittleEndian.from!(uint)(h1); | 175 result[4..8] = ByteConverter.LittleEndian.from!(uint)(h1); |
176 result[8..12] = ByteConverter.LittleEndian.from!(uint)(h2); | 176 result[8..12] = ByteConverter.LittleEndian.from!(uint)(h2); |
200 return h; | 200 return h; |
201 } | 201 } |
202 | 202 |
203 debug (UnitTest) | 203 debug (UnitTest) |
204 { | 204 { |
205 // Found in Tango <3 | |
206 unittest | 205 unittest |
207 { | 206 { |
208 static const char[][] test_inputs = [ | 207 static string[] test_inputs = [ |
209 "", | 208 "", |
210 "a", | 209 "a", |
211 "abc", | 210 "abc", |
212 "message digest", | 211 "message digest", |
213 "abcdefghijklmnopqrstuvwxyz", | 212 "abcdefghijklmnopqrstuvwxyz", |
214 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | 213 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
215 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" | 214 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" |
216 ]; | 215 ]; |
217 | 216 |
218 static const char[][] test_results = [ | 217 static string[] test_results = [ |
219 "31d6cfe0d16ae931b73c59d7e0c089c0", | 218 "31d6cfe0d16ae931b73c59d7e0c089c0", |
220 "bde52cb31de33e46245e05fbdbd6fb24", | 219 "bde52cb31de33e46245e05fbdbd6fb24", |
221 "a448017aaf21d8525fc10ae87aa6729d", | 220 "a448017aaf21d8525fc10ae87aa6729d", |
222 "d9130a8164549fe818874806e1c7014b", | 221 "d9130a8164549fe818874806e1c7014b", |
223 "d79e1c308aa5bbcdeea8ed63df412da9", | 222 "d79e1c308aa5bbcdeea8ed63df412da9", |
224 "043f8582f241db351ce627e153e7f0e4", | 223 "043f8582f241db351ce627e153e7f0e4", |
225 "e33b4ddc9c38f2199c3e7b164fcc0536" | 224 "e33b4ddc9c38f2199c3e7b164fcc0536" |
226 ]; | 225 ]; |
227 | 226 |
228 MD4 h = new MD4(); | 227 MD4 h = new MD4(); |
229 foreach (uint i, char[] input; test_inputs) | 228 foreach (uint i, string input; test_inputs) |
230 { | 229 { |
231 h.update(input); | 230 h.update(input); |
232 char[] digest = h.hexDigest(); | 231 string digest = h.hexDigest(); |
233 assert(digest == test_results[i], | 232 assert(digest == test_results[i], |
234 h.name~": ("~digest~") != ("~test_results[i]~")"); | 233 h.name~": ("~digest~") != ("~test_results[i]~")"); |
235 } | 234 } |
236 } | 235 } |
237 } | 236 } |