changeset 30:21847420b1ac

Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
author Thomas Dixon <reikon@reikon.us>
date Mon, 11 May 2009 15:32:00 -0400
parents b9ba770b8f16
children f429c5e9dd69
files dcrypt/crypto/Hash.d dcrypt/crypto/macs/HMAC.d dcrypt/crypto/prngs/PBKDF2.d dcrypt/misc/Bitwise.d dcrypt/misc/ByteConverter.d dcrypt/misc/Checksum.d dcrypt/misc/checksums/Adler32.d dcrypt/misc/checksums/CRC32.d
diffstat 8 files changed, 121 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/dcrypt/crypto/Hash.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/crypto/Hash.d	Mon May 11 15:32:00 2009 -0400
@@ -59,16 +59,28 @@
     Hash update(void[] input_)
     {
         ubyte[] input = cast(ubyte[]) input_;
-        foreach (ubyte i; input)
+        uint i, partLength;
+        
+        index = bytes & (blockSize - 1);
+        bytes += input.length;
+        
+        partLength = blockSize - index;
+        
+        if (input.length >= partLength)
         {
-            bytes++;
-            buffer[index++] = i;
-            if (index == blockSize)
-            {
-                transform(buffer);
-                index = 0;
-            }   
+            buffer[index..index+partLength] = input[0..partLength];
+            transform(buffer);
+            
+            for (i = partLength; i + (blockSize - 1) < input.length; i+=blockSize)
+                transform(input[i..i+blockSize]);
+                
+            index = 0;
         }
+        else
+            i = 0;
+            
+        if (input.length - i)
+            buffer[index..index+(input.length-i)] = input[i..input.length];
         
         return this;
     }
@@ -86,6 +98,7 @@
     protected void padMessage(uint mode)
     {
         ulong bits = bytes << 3;
+        index = bytes & (blockSize - 1);
         
         // Add the pad marker
         buffer[index++] = ((mode == MODE_TIGER) ? 0x01 : 0x80);
--- a/dcrypt/crypto/macs/HMAC.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/crypto/macs/HMAC.d	Mon May 11 15:32:00 2009 -0400
@@ -152,11 +152,11 @@
                 "6b2d53697a65204b6579202d2048617368204b6579204669727374"
             ];
             
-            static const int[] test_repeat = [
+            static int[] test_repeat = [
                 1, 1, 50, 1
             ];
             
-            static const string[] test_results = [
+            static string[] test_results = [
                 "b617318655057264e28bc0b6fb378c8ef146be00",
                 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
                 "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
--- a/dcrypt/crypto/prngs/PBKDF2.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/crypto/prngs/PBKDF2.d	Mon May 11 15:32:00 2009 -0400
@@ -136,11 +136,11 @@
                 "pass phrase exceeds block size"
             ];
             
-            static const int[] test_iterations = [
+            static int[] test_iterations = [
                 1, 1200, 1200, 1200
             ];
             
-            static const string[] test_results = [
+            static string[] test_results = [
                 "cdedb5281bb2f801565a1122b2563515",
                 "5c08eb61fdf71e4e4ec3cf6ba1f5512b"~
                 "a7e52ddbc5e5142f708a31e2e62b1e13",
--- a/dcrypt/misc/Bitwise.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/misc/Bitwise.d	Mon May 11 15:32:00 2009 -0400
@@ -9,37 +9,46 @@
 module dcrypt.misc.Bitwise;
 
 /** Common bitwise operations */
-struct Bitwise {
+struct Bitwise
+{
     
-    static uint rotateLeft(uint x, int y) {
+    static uint rotateLeft(uint x, int y)
+    {
         return (x << y) | (x >> (32-y));
     }
     
-    static uint rotateLeft(uint x, uint y) {
+    static uint rotateLeft(uint x, uint y)
+    {
         return (x << y) | (x >> (32u-y));
     }
     
-    static ulong rotateLeft(ulong x, int y) {
+    static ulong rotateLeft(ulong x, int y)
+    {
         return (x << y) | (x >> (64-y));
     }
     
-    static ulong rotateLeft(ulong x, uint y) {
+    static ulong rotateLeft(ulong x, uint y)
+    {
         return (x << y) | (x >> (64u-y));
     }
     
-    static uint rotateRight(uint x, int y) {
+    static uint rotateRight(uint x, int y)
+    {
         return (x >> y) | (x << (32-y));  
     }
     
-    static uint rotateRight(uint x, uint y) {
+    static uint rotateRight(uint x, uint y)
+    {
         return (x >> y) | (x << (32u-y));    
     }
     
-    static ulong rotateRight(ulong x, int y) {
+    static ulong rotateRight(ulong x, int y)
+    {
         return (x >> y) | (x << (64-y));    
     }
     
-    static ulong rotateRight(ulong x, uint y) {
+    static ulong rotateRight(ulong x, uint y)
+    {
         return (x >> y) | (x << (64u-y));    
     }
 }
--- a/dcrypt/misc/ByteConverter.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/misc/ByteConverter.d	Mon May 11 15:32:00 2009 -0400
@@ -9,11 +9,13 @@
 module dcrypt.misc.ByteConverter;
 
 /** Converts between integral types and unsigned byte arrays */
-struct ByteConverter {
-    private static const string hexits = "0123456789abcdef";
+struct ByteConverter
+{
+    private static string hexits = "0123456789abcdef";
     
     /** Conversions between little endian integrals and bytes */
-    struct LittleEndian {
+    struct LittleEndian
+    {
         /**
          * Converts the supplied array to integral type T
          * 
@@ -24,18 +26,21 @@
          *     A integral of type T created with the supplied bytes placed
          *     in the specified byte order.
          */
-        static T to(T)(void[] x_) {
+        static T to(T)(void[] x_)
+        {
             ubyte[] x = cast(ubyte[])x_;
             
             T result = ((x[0] & 0xff)       |
                        ((x[1] & 0xff) << 8));
                        
-            static if (T.sizeof >= int.sizeof) {
+            static if (T.sizeof >= int.sizeof)
+            {
                 result |= ((x[2] & 0xff) << 16) |
                           ((x[3] & 0xff) << 24);
             }
             
-            static if (T.sizeof >= long.sizeof) {
+            static if (T.sizeof >= long.sizeof)
+            {
                 result |= (cast(T)(x[4] & 0xff) << 32) |
                           (cast(T)(x[5] & 0xff) << 40) |
                           (cast(T)(x[6] & 0xff) << 48) |
@@ -55,18 +60,21 @@
          *     Integral input of type T split into its respective bytes
          *     with the bytes placed in the specified byte order.
          */
-        static ubyte[] from(T)(T input) {
+        static ubyte[] from(T)(T input)
+        {
             ubyte[] output = new ubyte[T.sizeof];
             
             output[0] = cast(ubyte)(input);
             output[1] = cast(ubyte)(input >> 8);
             
-            static if (T.sizeof >= int.sizeof) {
+            static if (T.sizeof >= int.sizeof)
+            {
                 output[2] = cast(ubyte)(input >> 16);
                 output[3] = cast(ubyte)(input >> 24);
             }
             
-            static if (T.sizeof >= long.sizeof) {
+            static if (T.sizeof >= long.sizeof)
+            {
                 output[4] = cast(ubyte)(input >> 32);
                 output[5] = cast(ubyte)(input >> 40);
                 output[6] = cast(ubyte)(input >> 48);
@@ -78,20 +86,27 @@
     }
     
     /** Conversions between big endian integrals and bytes */
-    struct BigEndian {
+    struct BigEndian
+    {
         
-        static T to(T)(void[] x_) {
+        static T to(T)(void[] x_)
+        {
             ubyte[] x = cast(ubyte[])x_;
             
-            static if (is(T == ushort) || is(T == short)) {
+            static if (is(T == ushort) || is(T == short))
+            {
                 return cast(T) (((x[0] & 0xff) << 8) |
                                  (x[1] & 0xff));
-            } else static if (is(T == uint) || is(T == int)) {
+            }
+            else static if (is(T == uint) || is(T == int))
+            {
                 return cast(T) (((x[0] & 0xff) << 24) |
                                 ((x[1] & 0xff) << 16) |
                                 ((x[2] & 0xff) << 8)  |
                                  (x[3] & 0xff));
-            } else static if (is(T == ulong) || is(T == long)) {
+            }
+            else static if (is(T == ulong) || is(T == long))
+            {
                 return cast(T) ((cast(T)(x[0] & 0xff) << 56) |
                                 (cast(T)(x[1] & 0xff) << 48) |
                                 (cast(T)(x[2] & 0xff) << 40) |
@@ -103,10 +118,12 @@
             }
         }
         
-        static ubyte[] from(T)(T input) {
+        static ubyte[] from(T)(T input)
+        {
             ubyte[] output = new ubyte[T.sizeof];
             
-            static if (T.sizeof == long.sizeof) {
+            static if (T.sizeof == long.sizeof)
+            {
                 output[0] = cast(ubyte)(input >> 56);
                 output[1] = cast(ubyte)(input >> 48);
                 output[2] = cast(ubyte)(input >> 40);
@@ -115,42 +132,53 @@
                 output[5] = cast(ubyte)(input >> 16);
                 output[6] = cast(ubyte)(input >> 8);
                 output[7] = cast(ubyte)(input);
-            } else static if (T.sizeof == int.sizeof) {
+            }
+            else static if (T.sizeof == int.sizeof)
+            {
                 output[0] = cast(ubyte)(input >> 24);
                 output[1] = cast(ubyte)(input >> 16);
                 output[2] = cast(ubyte)(input >> 8);
                 output[3] = cast(ubyte)(input);
-            } else static if (T.sizeof == short.sizeof) {
+            }
+            else static if (T.sizeof == short.sizeof)
+            {
                 output[0] = cast(ubyte)(input >> 8);
                 output[1] = cast(ubyte)(input);
             }
+            
             return output;
         }
     }
 
-    static char[] hexEncode(void[] input_) {
+    static string hexEncode(void[] input_)
+    {
         ubyte[] input = cast(ubyte[])input_;
         char[] output = new char[input.length<<1];
         
         int i = 0;
-        foreach (ubyte j; input) { 
+        foreach (ubyte j; input)
+        { 
             output[i++] = hexits[j>>4];
             output[i++] = hexits[j&0xf];
         }
-        return output;    
+        
+        return cast(string)output;    
     }
     
-    static ubyte[] hexDecode(string input) {
+    static ubyte[] hexDecode(string input)
+    {
         ubyte[] output = new ubyte[input.length>>1];
         
         static ubyte[char] hexitIndex;
         for (int i = 0; i < hexits.length; i++)
             hexitIndex[hexits[i]] = i;
             
-        for (int i = 0, j = 0; i < output.length; i++) {
+        for (int i = 0, j = 0; i < output.length; i++)
+        {
             output[i] = hexitIndex[input[j++]] << 4;
             output[i] |= hexitIndex[input[j++]]; 
         }
+        
         return output;
     }
 }
--- a/dcrypt/misc/Checksum.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/misc/Checksum.d	Mon May 11 15:32:00 2009 -0400
@@ -9,7 +9,8 @@
 module dcrypt.misc.Checksum;
 
 /** Base class for 32-bit checksums */
-abstract class Checksum {
+abstract class Checksum
+{
     /**
      * Compute a checksum.
      * 
@@ -23,4 +24,4 @@
     
     /** Returns: The name of the checksum algorithm. */
     string name();
-}
\ No newline at end of file
+}
--- a/dcrypt/misc/checksums/Adler32.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/misc/checksums/Adler32.d	Mon May 11 15:32:00 2009 -0400
@@ -16,16 +16,19 @@
  * Conforms: RFC 1950
  * References: http://tools.ietf.org/html/rfc1950#page-10
  */
-class Adler32 : Checksum {
+class Adler32 : Checksum
+{
     private static const uint BASE = 65521;
     
-    uint compute(void[] input_, uint start=1) {
+    uint compute(void[] input_, uint start=1)
+    {
         ubyte[] input = cast(ubyte[])input_;
         uint adler = start,
              s1 = adler & 0xffff,
              s2 = (adler >> 16) & 0xffff;
         
-        foreach (ubyte i; input) {
+        foreach (ubyte i; input)
+        {
             s1 = (s1 + i) % BASE;
             s2 = (s2 + s1) % BASE;
         }
@@ -33,12 +36,15 @@
         return (s2 << 16) + s1;
     }
     
-    string name() {
+    string name()
+    {
         return "Adler32";
     }
     
-    debug (UnitTest) {
-        unittest {
+    debug (UnitTest)
+    {
+        unittest
+        {
             static string[] test_inputs = [
                 "",
                 "a",
@@ -58,4 +64,4 @@
                 assert(adler32.compute(j) == test_results[i], adler32.name);
         }
     }
-}
\ No newline at end of file
+}
--- a/dcrypt/misc/checksums/CRC32.d	Mon May 11 01:39:19 2009 -0400
+++ b/dcrypt/misc/checksums/CRC32.d	Mon May 11 15:32:00 2009 -0400
@@ -10,7 +10,8 @@
 
 import dcrypt.misc.Checksum;
 
-class CRC32 : Checksum {
+class CRC32 : Checksum
+{
     // Polynomial used is 0xedb88320.
     private static const uint[256] table = [
         0x00000000u, 0x77073096u, 0xee0e612cu, 0x990951bau, 0x076dc419u,
@@ -67,7 +68,8 @@
         0x2d02ef8du
     ];
     
-    uint compute(void[] input_, uint start=0) {
+    uint compute(void[] input_, uint start=0)
+    {
         ubyte[] input = cast(ubyte[])input_;
         uint crc = (start ^ 0xffffffff);
         
@@ -77,12 +79,15 @@
         return (crc ^ 0xffffffff);
     }
     
-    string name() {
+    string name()
+    {
         return "CRC32";
     }
     
-    debug (UnitTest) {
-        unittest {
+    debug (UnitTest)
+    {
+        unittest
+        {
             static string[] test_inputs = [
                 "",
                 "a",
@@ -102,4 +107,4 @@
                 assert(crc32.compute(j) == test_results[i], crc32.name);
         }
     }
-}
\ No newline at end of file
+}