Mercurial > projects > dcrypt
comparison dcrypt/crypto/hashes/SHA512.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 SHA512 : Hash | 19 class SHA512 : Hash |
20 { | 20 { |
21 private const ulong[] K = [ | 21 private static const ulong[] K = [ |
22 0x428a2f98d728ae22u, 0x7137449123ef65cdu, 0xb5c0fbcfec4d3b2fu, 0xe9b5dba58189dbbcu, | 22 0x428a2f98d728ae22u, 0x7137449123ef65cdu, 0xb5c0fbcfec4d3b2fu, 0xe9b5dba58189dbbcu, |
23 0x3956c25bf348b538u, 0x59f111f1b605d019u, 0x923f82a4af194f9bu, 0xab1c5ed5da6d8118u, | 23 0x3956c25bf348b538u, 0x59f111f1b605d019u, 0x923f82a4af194f9bu, 0xab1c5ed5da6d8118u, |
24 0xd807aa98a3030242u, 0x12835b0145706fbeu, 0x243185be4ee4b28cu, 0x550c7dc3d5ffb4e2u, | 24 0xd807aa98a3030242u, 0x12835b0145706fbeu, 0x243185be4ee4b28cu, 0x550c7dc3d5ffb4e2u, |
25 0x72be5d74f27b896fu, 0x80deb1fe3b1696b1u, 0x9bdc06a725c71235u, 0xc19bf174cf692694u, | 25 0x72be5d74f27b896fu, 0x80deb1fe3b1696b1u, 0x9bdc06a725c71235u, 0xc19bf174cf692694u, |
26 0xe49b69c19ef14ad2u, 0xefbe4786384f25e3u, 0x0fc19dc68b8cd5b5u, 0x240ca1cc77ac9c65u, | 26 0xe49b69c19ef14ad2u, 0xefbe4786384f25e3u, 0x0fc19dc68b8cd5b5u, 0x240ca1cc77ac9c65u, |
39 0x06f067aa72176fbau, 0x0a637dc5a2c898a6u, 0x113f9804bef90daeu, 0x1b710b35131c471bu, | 39 0x06f067aa72176fbau, 0x0a637dc5a2c898a6u, 0x113f9804bef90daeu, 0x1b710b35131c471bu, |
40 0x28db77f523047d84u, 0x32caab7b40c72493u, 0x3c9ebe0a15c9bebcu, 0x431d67c49c100d4cu, | 40 0x28db77f523047d84u, 0x32caab7b40c72493u, 0x3c9ebe0a15c9bebcu, 0x431d67c49c100d4cu, |
41 0x4cc5d4becb3e42b6u, 0x597f299cfc657e2au, 0x5fcb6fab3ad6faecu, 0x6c44198c4a475817u | 41 0x4cc5d4becb3e42b6u, 0x597f299cfc657e2au, 0x5fcb6fab3ad6faecu, 0x6c44198c4a475817u |
42 ]; | 42 ]; |
43 | 43 |
44 protected ulong h0, h1, h2, h3, h4, h5, h6, h7; | 44 protected ulong h0, h1, h2, h3, h4, h5, h6, h7; |
45 | 45 |
46 this (void[] input_=null) | 46 this (void[] input_=null) |
47 { | 47 { |
48 reset(); | 48 reset(); |
49 super(input_); | 49 super(input_); |
57 uint digestSize() | 57 uint digestSize() |
58 { | 58 { |
59 return 64; | 59 return 64; |
60 } | 60 } |
61 | 61 |
62 char[] name() | 62 string name() |
63 { | 63 { |
64 return "SHA512"; | 64 return "SHA512"; |
65 } | 65 } |
66 | 66 |
67 void transform(ubyte[] input) | 67 void transform(ubyte[] input) |
141 return Bitwise.rotateRight(x,19)^Bitwise.rotateRight(x,61)^(x >> 6); | 141 return Bitwise.rotateRight(x,19)^Bitwise.rotateRight(x,61)^(x >> 6); |
142 } | 142 } |
143 | 143 |
144 ubyte[] digest() | 144 ubyte[] digest() |
145 { | 145 { |
146 padMessage(MODE_SHA); | 146 padMessage(MODE_SHA); |
147 ubyte[] result = new ubyte[digestSize]; | 147 ubyte[] result = new ubyte[digestSize]; |
148 | 148 |
149 result[0..8] = ByteConverter.BigEndian.from!(ulong)(h0); | 149 result[0..8] = ByteConverter.BigEndian.from!(ulong)(h0); |
150 result[8..16] = ByteConverter.BigEndian.from!(ulong)(h1); | 150 result[8..16] = ByteConverter.BigEndian.from!(ulong)(h1); |
151 result[16..24] = ByteConverter.BigEndian.from!(ulong)(h2); | 151 result[16..24] = ByteConverter.BigEndian.from!(ulong)(h2); |
189 | 189 |
190 debug (UnitTest) | 190 debug (UnitTest) |
191 { | 191 { |
192 unittest | 192 unittest |
193 { | 193 { |
194 static const char[][] test_inputs = [ | 194 static string[] test_inputs = [ |
195 "", | 195 "", |
196 "abc", | 196 "abc", |
197 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~ | 197 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~ |
198 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", | 198 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
199 "a" | 199 "a" |
200 ]; | 200 ]; |
201 | 201 |
202 static const int[] test_repeat = [ | 202 static int[] test_repeat = [ |
203 1, 1, 1, 1000000 | 203 1, 1, 1, 1000000 |
204 ]; | 204 ]; |
205 | 205 |
206 static const char[][] test_results = [ | 206 static string[] test_results = [ |
207 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"~ | 207 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"~ |
208 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | 208 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", |
209 | 209 |
210 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"~ | 210 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"~ |
211 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", | 211 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", |
216 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"~ | 216 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"~ |
217 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b" | 217 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b" |
218 ]; | 218 ]; |
219 | 219 |
220 SHA512 h = new SHA512(); | 220 SHA512 h = new SHA512(); |
221 foreach (uint i, char[] input; test_inputs) | 221 foreach (uint i, string input; test_inputs) |
222 { | 222 { |
223 for (int j = 0; j < test_repeat[i]; j++) | 223 for (int j = 0; j < test_repeat[i]; j++) |
224 h.update(input); | 224 h.update(input); |
225 char[] digest = h.hexDigest(); | 225 string digest = h.hexDigest(); |
226 assert(digest == test_results[i], | 226 assert(digest == test_results[i], |
227 h.name~": ("~digest~") != ("~test_results[i]~")"); | 227 h.name~": ("~digest~") != ("~test_results[i]~")"); |
228 } | 228 } |
229 } | 229 } |
230 } | 230 } |