Mercurial > projects > dcrypt
comparison dcrypt/crypto/hashes/SHA256.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 |
---|---|
16 * Conforms: FIPS-180-2 | 16 * Conforms: FIPS-180-2 |
17 * References: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf | 17 * References: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
18 */ | 18 */ |
19 class SHA256 : Hash | 19 class SHA256 : Hash |
20 { | 20 { |
21 private const uint[] K = [ | 21 private static const uint[] K = [ |
22 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, | 22 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, |
23 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, | 23 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, |
24 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, | 24 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, |
25 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u, | 25 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u, |
26 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, | 26 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, |
35 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u, | 35 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u, |
36 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, | 36 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, |
37 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u | 37 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u |
38 ]; | 38 ]; |
39 | 39 |
40 protected uint h0, h1, h2, h3, h4, h5, h6, h7; | 40 protected uint h0, h1, h2, h3, h4, h5, h6, h7; |
41 | 41 |
42 this (void[] input_=null) | 42 this (void[] input_=null) |
43 { | 43 { |
44 reset(); | 44 reset(); |
45 super(input_); | 45 super(input_); |
53 uint digestSize() | 53 uint digestSize() |
54 { | 54 { |
55 return 32; | 55 return 32; |
56 } | 56 } |
57 | 57 |
58 char[] name() | 58 string name() |
59 { | 59 { |
60 return "SHA256"; | 60 return "SHA256"; |
61 } | 61 } |
62 | 62 |
63 void transform(ubyte[] input) | 63 void transform(ubyte[] input) |
133 return Bitwise.rotateRight(x,17)^Bitwise.rotateRight(x,19)^(x >> 10); | 133 return Bitwise.rotateRight(x,17)^Bitwise.rotateRight(x,19)^(x >> 10); |
134 } | 134 } |
135 | 135 |
136 ubyte[] digest() | 136 ubyte[] digest() |
137 { | 137 { |
138 padMessage(MODE_SHA); | 138 padMessage(MODE_SHA); |
139 ubyte[] result = new ubyte[digestSize]; | 139 ubyte[] result = new ubyte[digestSize]; |
140 | 140 |
141 result[0..4] = ByteConverter.BigEndian.from!(uint)(h0); | 141 result[0..4] = ByteConverter.BigEndian.from!(uint)(h0); |
142 result[4..8] = ByteConverter.BigEndian.from!(uint)(h1); | 142 result[4..8] = ByteConverter.BigEndian.from!(uint)(h1); |
143 result[8..12] = ByteConverter.BigEndian.from!(uint)(h2); | 143 result[8..12] = ByteConverter.BigEndian.from!(uint)(h2); |
181 | 181 |
182 debug (UnitTest) | 182 debug (UnitTest) |
183 { | 183 { |
184 unittest | 184 unittest |
185 { | 185 { |
186 static const char[][] test_inputs = [ | 186 static string[] test_inputs = [ |
187 "", | 187 "", |
188 "abc", | 188 "abc", |
189 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | 189 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
190 "a" | 190 "a" |
191 ]; | 191 ]; |
192 | 192 |
193 static const int[] test_repeat = [ | 193 static int[] test_repeat = [ |
194 1, 1, 1, 1000000 | 194 1, 1, 1, 1000000 |
195 ]; | 195 ]; |
196 | 196 |
197 static const char[][] test_results = [ | 197 static string[] test_results = [ |
198 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | 198 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", |
199 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", | 199 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", |
200 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", | 200 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", |
201 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" | 201 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" |
202 ]; | 202 ]; |
203 | 203 |
204 SHA256 h = new SHA256(); | 204 SHA256 h = new SHA256(); |
205 foreach (uint i, char[] input; test_inputs) | 205 foreach (uint i, string input; test_inputs) |
206 { | 206 { |
207 for (int j = 0; j < test_repeat[i]; j++) | 207 for (int j = 0; j < test_repeat[i]; j++) |
208 h.update(input); | 208 h.update(input); |
209 char[] digest = h.hexDigest(); | 209 string digest = h.hexDigest(); |
210 assert(digest == test_results[i], | 210 assert(digest == test_results[i], |
211 h.name~": ("~digest~") != ("~test_results[i]~")"); | 211 h.name~": ("~digest~") != ("~test_results[i]~")"); |
212 } | 212 } |
213 } | 213 } |
214 } | 214 } |