Mercurial > projects > dcrypt
diff dcrypt/crypto/hashes/SHA256.d @ 27:8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 09 May 2009 23:29:20 -0400 |
parents | 176c933827a8 |
children | ad687db713a4 |
line wrap: on
line diff
--- a/dcrypt/crypto/hashes/SHA256.d Sun Mar 01 13:06:48 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA256.d Sat May 09 23:29:20 2009 -0400 @@ -16,7 +16,8 @@ * Conforms: FIPS-180-2 * References: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf */ -class SHA256 : Hash { +class SHA256 : Hash +{ private const uint[] K = [ 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, @@ -35,26 +36,32 @@ 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u ]; + protected uint h0, h1, h2, h3, h4, h5, h6, h7; - this (void[] input_=null) { + this (void[] input_=null) + { reset(); super(input_); } - uint blockSize() { + uint blockSize() + { return 64; } - uint digestSize() { + uint digestSize() + { return 32; } - char[] name() { + char[] name() + { return "SHA256"; } - void transform(ubyte[] input) { + void transform(ubyte[] input) + { uint[] w = new uint[64]; for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof) @@ -72,7 +79,8 @@ g = h6, h = h7; - for (int i = 0; i < 64; i++) { + for (int i = 0; i < 64; i++) + { uint t1 = h + sum1(e) + ch(e,f,g) + K[i] + w[i], t2 = sum0(a) + maj(a,b,c); h = g; @@ -95,31 +103,38 @@ h7 += h; } - private uint ch(uint x, uint y, uint z) { - return (x&y)^(~x&z); + private uint ch(uint x, uint y, uint z) + { + return (z ^ (x & (y ^ z))); } - private uint maj(uint x, uint y, uint z) { - return (x&y)^(x&z)^(y&z); + private uint maj(uint x, uint y, uint z) + { + return ((x & y) | (z & (x ^ y))); } - private uint sum0(uint x) { + private uint sum0(uint x) + { return Bitwise.rotateRight(x,2)^Bitwise.rotateRight(x,13)^Bitwise.rotateRight(x,22); } - private uint sum1(uint x) { + private uint sum1(uint x) + { return Bitwise.rotateRight(x,6)^Bitwise.rotateRight(x,11)^Bitwise.rotateRight(x,25); } - private uint theta0(uint x) { + private uint theta0(uint x) + { return Bitwise.rotateRight(x,7)^Bitwise.rotateRight(x,18)^(x >> 3); } - private uint theta1(uint x) { + private uint theta1(uint x) + { return Bitwise.rotateRight(x,17)^Bitwise.rotateRight(x,19)^(x >> 10); } - ubyte[] digest() { + ubyte[] digest() + { padMessage(MODE_SHA); ubyte[] result = new ubyte[digestSize]; @@ -136,7 +151,8 @@ return result; } - void reset() { + void reset() + { super.reset(); h0 = 0x6a09e667u; h1 = 0xbb67ae85u; @@ -148,7 +164,8 @@ h7 = 0x5be0cd19u; } - SHA256 copy() { + SHA256 copy() + { SHA256 h = new SHA256(buffer[0..index]); h.bytes = bytes; h.h0 = h0; @@ -162,8 +179,10 @@ return h; } - debug (UnitTest) { - unittest { + debug (UnitTest) + { + unittest + { static const char[][] test_inputs = [ "", "abc", @@ -183,7 +202,8 @@ ]; SHA256 h = new SHA256(); - foreach (uint i, char[] input; test_inputs) { + foreach (uint i, char[] input; test_inputs) + { for (int j = 0; j < test_repeat[i]; j++) h.update(input); char[] digest = h.hexDigest();