diff dcrypt/crypto/hashes/SHA512.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/SHA512.d	Sun Mar 01 13:06:48 2009 -0500
+++ b/dcrypt/crypto/hashes/SHA512.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 SHA512 : Hash {
+class SHA512 : Hash
+{
     private const ulong[] K = [
         0x428a2f98d728ae22u, 0x7137449123ef65cdu, 0xb5c0fbcfec4d3b2fu, 0xe9b5dba58189dbbcu, 
         0x3956c25bf348b538u, 0x59f111f1b605d019u, 0x923f82a4af194f9bu, 0xab1c5ed5da6d8118u, 
@@ -39,26 +40,32 @@
         0x28db77f523047d84u, 0x32caab7b40c72493u, 0x3c9ebe0a15c9bebcu, 0x431d67c49c100d4cu, 
         0x4cc5d4becb3e42b6u, 0x597f299cfc657e2au, 0x5fcb6fab3ad6faecu, 0x6c44198c4a475817u
     ];
+    
 	protected ulong h0, h1, h2, h3, h4, h5, h6, h7;
     
-    this (void[] input_=null) {
+    this (void[] input_=null)
+    {
         reset();
         super(input_);
     }
 
-    uint blockSize() {
+    uint blockSize()
+    {
         return 128;
     }
     
-    uint digestSize() {
+    uint digestSize()
+    {
         return 64;
     }
     
-    char[] name() {
+    char[] name()
+    {
         return "SHA512";
     }
     
-    void transform(ubyte[] input) {
+    void transform(ubyte[] input)
+    {
         ulong[] w = new ulong[80];
         
         for (int i = 0, j = 0; i < 16; i++,j+=long.sizeof)
@@ -76,7 +83,8 @@
               g = h6,
               h = h7;
         
-        for (int i = 0; i < 80; i++) {
+        for (int i = 0; i < 80; i++)
+        {
             ulong t1 = h + sum1(e) + ch(e,f,g) + K[i] + w[i],
                   t2 = sum0(a) + maj(a,b,c);
             h = g;
@@ -99,35 +107,42 @@
         h7 += h;
     }
     
-    private ulong ch(ulong x, ulong y, ulong z) {
+    private ulong ch(ulong x, ulong y, ulong z)
+    {
             return (x&y)^(~x&z);
     }
     
-    private ulong maj(ulong x, ulong y, ulong z) {
+    private ulong maj(ulong x, ulong y, ulong z)
+    {
             return (x&y)^(x&z)^(y&z);
     }
 
-    private ulong sum0(ulong x) {
+    private ulong sum0(ulong x)
+    {
             return (Bitwise.rotateRight(x,28)^
                     Bitwise.rotateRight(x,34)^
                     Bitwise.rotateRight(x,39));
     }
 
-    private ulong sum1(ulong x) {
+    private ulong sum1(ulong x)
+    {
             return (Bitwise.rotateRight(x,14)^
                     Bitwise.rotateRight(x,18)^
                     Bitwise.rotateRight(x,41));
     }
 
-    private ulong theta0(ulong x) {
+    private ulong theta0(ulong x)
+    {
         return Bitwise.rotateRight(x,1)^Bitwise.rotateRight(x,8)^(x >> 7);
     }
 
-    private ulong theta1(ulong x) {
+    private ulong theta1(ulong x)
+    {
         return Bitwise.rotateRight(x,19)^Bitwise.rotateRight(x,61)^(x >> 6);
     }
     
-    ubyte[] digest() {
+    ubyte[] digest()
+    {
     	padMessage(MODE_SHA);
         ubyte[] result = new ubyte[digestSize];
         
@@ -144,7 +159,8 @@
         return result;
     }
 
-    void reset() {
+    void reset()
+    {
         super.reset();
         h0 = 0x6a09e667f3bcc908u;
         h1 = 0xbb67ae8584caa73bu;
@@ -156,7 +172,8 @@
         h7 = 0x5be0cd19137e2179u;
     }
     
-    SHA512 copy() {
+    SHA512 copy()
+    {
         SHA512 h = new SHA512(buffer[0..index]);
         h.bytes = bytes;
         h.h0 = h0;
@@ -170,8 +187,10 @@
         return h;
     }
     
-    debug (UnitTest) {
-        unittest {
+    debug (UnitTest)
+    {
+        unittest
+        {
             static const char[][] test_inputs = [
                 "",
                 "abc",
@@ -199,7 +218,8 @@
             ];
 
             SHA512 h = new SHA512();
-            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();