Mercurial > projects > dcrypt
diff dcrypt/crypto/hashes/SHA1.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/SHA1.d Sun Mar 01 13:06:48 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA1.d Sat May 09 23:29:20 2009 -0400 @@ -17,27 +17,33 @@ * References: http://www.itl.nist.gov/fipspubs/fip180-1.htm * Bugs: SHA-1 is not cryptographically secure. */ -class SHA1 : Hash { +class SHA1 : Hash +{ protected uint h0, h1, h2, h3, h4; - this (void[] input_=null) { + this (void[] input_=null) + { reset(); super(input_); } - uint blockSize() { + uint blockSize() + { return 64; } - uint digestSize() { + uint digestSize() + { return 20; } - char[] name() { + char[] name() + { return "SHA1"; } - void transform(ubyte[] input) { + void transform(ubyte[] input) + { uint[] w = new uint[80]; for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof) @@ -53,7 +59,8 @@ e = h4; int i = 0; - for (; i < 20;) { + for (; i < 20;) + { e += Bitwise.rotateLeft(a, 5) + f0(b, c, d) + w[i++]; b = Bitwise.rotateLeft(b, 30); @@ -70,7 +77,8 @@ c = Bitwise.rotateLeft(c, 30); } - for (; i < 40;) { + for (; i < 40;) + { e += Bitwise.rotateLeft(a, 5) + f1(b, c, d) + w[i++]; b = Bitwise.rotateLeft(b, 30); @@ -87,7 +95,8 @@ c = Bitwise.rotateLeft(c, 30); } - for (; i < 60;) { + for (; i < 60;) + { e += Bitwise.rotateLeft(a, 5) + f2(b, c, d) + w[i++]; b = Bitwise.rotateLeft(b, 30); @@ -104,7 +113,8 @@ c = Bitwise.rotateLeft(c, 30); } - for (; i < 80;) { + for (; i < 80;) + { e += Bitwise.rotateLeft(a, 5) + f3(b, c, d) + w[i++]; b = Bitwise.rotateLeft(b, 30); @@ -128,23 +138,28 @@ h4 += e; } - private uint f0(uint x, uint y, uint z) { + private uint f0(uint x, uint y, uint z) + { return (z^(x&(y^z))) + 0x5a827999; } - private uint f1(uint x, uint y, uint z) { + private uint f1(uint x, uint y, uint z) + { return (x^y^z) + 0x6ed9eba1; } - private uint f2(uint x, uint y, uint z) { + private uint f2(uint x, uint y, uint z) + { return ((x&y)|(z&(x|y))) + 0x8f1bbcdc; } - private uint f3(uint x, uint y, uint z) { + private uint f3(uint x, uint y, uint z) + { return (x^y^z) + 0xca62c1d6; } - ubyte[] digest() { + ubyte[] digest() + { padMessage(MODE_SHA); ubyte[] result = new ubyte[digestSize]; @@ -158,7 +173,8 @@ return result; } - void reset() { + void reset() + { super.reset(); h0 = 0x67452301u; h1 = 0xefcdab89u; @@ -167,7 +183,8 @@ h4 = 0xc3d2e1f0u; } - SHA1 copy() { + SHA1 copy() + { SHA1 h = new SHA1(buffer[0..index]); h.bytes = bytes; h.h0 = h0; @@ -178,8 +195,10 @@ return h; } - debug (UnitTest) { - unittest { + debug (UnitTest) + { + unittest + { static const char[][] test_inputs = [ "", "abc", @@ -201,7 +220,8 @@ ]; SHA1 h = new SHA1(); - 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();