comparison dcrypt/crypto/hashes/SHA1.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.itl.nist.gov/fipspubs/fip180-1.htm 17 * References: http://www.itl.nist.gov/fipspubs/fip180-1.htm
18 * Bugs: SHA-1 is not cryptographically secure. 18 * Bugs: SHA-1 is not cryptographically secure.
19 */ 19 */
20 class SHA1 : Hash 20 class SHA1 : Hash
21 { 21 {
22 protected uint h0, h1, h2, h3, h4; 22 protected uint h0, h1, h2, h3, h4;
23 23
24 this (void[] input_=null) 24 this (void[] input_=null)
25 { 25 {
26 reset(); 26 reset();
27 super(input_); 27 super(input_);
35 uint digestSize() 35 uint digestSize()
36 { 36 {
37 return 20; 37 return 20;
38 } 38 }
39 39
40 char[] name() 40 string name()
41 { 41 {
42 return "SHA1"; 42 return "SHA1";
43 } 43 }
44 44
45 void transform(ubyte[] input) 45 void transform(ubyte[] input)
158 return (x^y^z) + 0xca62c1d6; 158 return (x^y^z) + 0xca62c1d6;
159 } 159 }
160 160
161 ubyte[] digest() 161 ubyte[] digest()
162 { 162 {
163 padMessage(MODE_SHA); 163 padMessage(MODE_SHA);
164 ubyte[] result = new ubyte[digestSize]; 164 ubyte[] result = new ubyte[digestSize];
165 165
166 result[0..4] = ByteConverter.BigEndian.from!(uint)(h0); 166 result[0..4] = ByteConverter.BigEndian.from!(uint)(h0);
167 result[4..8] = ByteConverter.BigEndian.from!(uint)(h1); 167 result[4..8] = ByteConverter.BigEndian.from!(uint)(h1);
168 result[8..12] = ByteConverter.BigEndian.from!(uint)(h2); 168 result[8..12] = ByteConverter.BigEndian.from!(uint)(h2);
197 197
198 debug (UnitTest) 198 debug (UnitTest)
199 { 199 {
200 unittest 200 unittest
201 { 201 {
202 static const char[][] test_inputs = [ 202 static string[] test_inputs = [
203 "", 203 "",
204 "abc", 204 "abc",
205 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 205 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
206 "a", 206 "a",
207 "0123456701234567012345670123456701234567012345670123456701234567" 207 "0123456701234567012345670123456701234567012345670123456701234567"
208 ]; 208 ];
209 209
210 static const int[] test_repeat = [ 210 static int[] test_repeat = [
211 1, 1, 1, 1000000, 10 211 1, 1, 1, 1000000, 10
212 ]; 212 ];
213 213
214 static const char[][] test_results = [ 214 static string[] test_results = [
215 "da39a3ee5e6b4b0d3255bfef95601890afd80709", 215 "da39a3ee5e6b4b0d3255bfef95601890afd80709",
216 "a9993e364706816aba3e25717850c26c9cd0d89d", 216 "a9993e364706816aba3e25717850c26c9cd0d89d",
217 "84983e441c3bd26ebaae4aa1f95129e5e54670f1", 217 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
218 "34aa973cd4c4daa4f61eeb2bdbad27316534016f", 218 "34aa973cd4c4daa4f61eeb2bdbad27316534016f",
219 "dea356a2cddd90c7a7ecedc5ebb563934f460452" 219 "dea356a2cddd90c7a7ecedc5ebb563934f460452"
220 ]; 220 ];
221 221
222 SHA1 h = new SHA1(); 222 SHA1 h = new SHA1();
223 foreach (uint i, char[] input; test_inputs) 223 foreach (uint i, string input; test_inputs)
224 { 224 {
225 for (int j = 0; j < test_repeat[i]; j++) 225 for (int j = 0; j < test_repeat[i]; j++)
226 h.update(input); 226 h.update(input);
227 char[] digest = h.hexDigest(); 227 string digest = h.hexDigest();
228 assert(digest == test_results[i], 228 assert(digest == test_results[i],
229 h.name~": ("~digest~") != ("~test_results[i]~")"); 229 h.name~": ("~digest~") != ("~test_results[i]~")");
230 } 230 }
231 } 231 }
232 } 232 }