changeset 2:71aae178f89a

Added copy() to hash functions. Modified some code style.
author Thomas Dixon <reikon@reikon.us>
date Wed, 13 Aug 2008 22:01:19 -0400
parents 483e4467b5f6
children a5789a7b3b3b
files dcrypt/crypto/BlockCipher.d dcrypt/crypto/Cipher.d dcrypt/crypto/Hash.d dcrypt/crypto/ciphers/Blowfish.d dcrypt/crypto/ciphers/RC4.d dcrypt/crypto/ciphers/RC6.d dcrypt/crypto/ciphers/TEA.d dcrypt/crypto/ciphers/XTEA.d dcrypt/crypto/hashes/MD5.d dcrypt/crypto/hashes/SHA1.d dcrypt/crypto/hashes/SHA224.d dcrypt/crypto/hashes/SHA256.d dcrypt/crypto/hashes/SHA384.d dcrypt/crypto/hashes/SHA512.d dcrypt/crypto/params/ParametersWithIV.d
diffstat 15 files changed, 107 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/dcrypt/crypto/BlockCipher.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/BlockCipher.d	Wed Aug 13 22:01:19 2008 -0400
@@ -12,7 +12,7 @@
 public import dcrypt.crypto.params.SymmetricKey;
 
 /** Interface for a standard block cipher. */
-abstract class BlockCipher : Cipher {
+interface BlockCipher : Cipher {
     
     /** Returns: The block size in bytes that this cipher will operate on. */
     uint blockSize();
@@ -32,7 +32,5 @@
      * Throws: dcrypt.crypto.errors.NotInitializedError if cipher
      *         was not initialized.
      */
-    uint processBlock(void[] input_, 
-                          uint inOff, void[] output_, uint outOff);
-
+    uint processBlock(void[] input_, uint inOff, void[] output_, uint outOff);
 }
--- a/dcrypt/crypto/Cipher.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/Cipher.d	Wed Aug 13 22:01:19 2008 -0400
@@ -27,12 +27,11 @@
      *     encrypt = True if we are encrypting.
      *     params  = Parameters to be passed to the cipher. (Key, rounds, etc.)
      */
-    abstract void init(bool encrypt, CipherParameters params);
+    void init(bool encrypt, CipherParameters params);
     
     /** Returns: The name of the algorithm of this cipher. */
-    abstract char[] name();
+    char[] name();
     
     /** Reset cipher to its state immediately subsequent the last init. */
-    abstract void reset();
-    
+    void reset();
 }
--- a/dcrypt/crypto/Hash.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/Hash.d	Wed Aug 13 22:01:19 2008 -0400
@@ -17,17 +17,20 @@
 		MODE_SHA,
 		MODE_TIGER
 	}
-	private ubyte[] buffer;
-	uint bytes,
-		 index;
-	
+    
+	protected {
+        ubyte[] buffer;
+        uint bytes,
+             index;
+    }
+        
 	this (void[] input_=null) {
 		buffer = new ubyte[blockSize];
         ubyte[] input = cast(ubyte[]) input_;
         if (input)
             update(input);
 	}
-
+    
 	/** Returns: The block size of the hash function in bytes. */
 	abstract uint blockSize();
 	
@@ -36,7 +39,10 @@
 	
 	/** Returns: The name of the algorithm we're implementing. */
 	abstract char[] name();
-	
+    
+    /** Returns: A copy of this hash object. */
+    abstract Hash copy();
+
 	/**
 	 * Introduce data into the hash function.
 	 * 
--- a/dcrypt/crypto/ciphers/Blowfish.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/ciphers/Blowfish.d	Wed Aug 13 22:01:19 2008 -0400
@@ -381,14 +381,14 @@
                 t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
-                        t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             
                 // Decryption
                 t.init(false, key);
                 t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
-                        t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/ciphers/RC4.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/ciphers/RC4.d	Wed Aug 13 22:01:19 2008 -0400
@@ -190,7 +190,7 @@
                 r.processBytes(Util.hexToUbytes(test_plaintexts[i]), 0, buffer.length, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
-                        r.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        r.name~": ("~result~") != ("~test_ciphertexts[i]~")");
                         
                 r.reset();
                 
@@ -198,7 +198,7 @@
                 r.processBytes(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer.length, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
-                        r.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        r.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/ciphers/RC6.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/ciphers/RC6.d	Wed Aug 13 22:01:19 2008 -0400
@@ -196,14 +196,14 @@
                 t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
-                        t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             
                 // Decryption
                 t.init(false, key);
                 t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
-                        t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/ciphers/TEA.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/ciphers/TEA.d	Wed Aug 13 22:01:19 2008 -0400
@@ -124,14 +124,14 @@
             t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0);
             result = Util.ubytesToHex(buffer);
             assert(result == test_ciphertexts[i],
-                    t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                    t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
 
             // Decryption
             t.init(false, key);
             t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0);
             result = Util.ubytesToHex(buffer);
             assert(result == test_plaintexts[i],
-                    t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                    t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
         }
     }
 }
--- a/dcrypt/crypto/ciphers/XTEA.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/ciphers/XTEA.d	Wed Aug 13 22:01:19 2008 -0400
@@ -151,14 +151,14 @@
                 t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
-                        t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
     
                 // Decryption
                 t.init(false, key);
                 t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
-                        t.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
+                        t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/hashes/MD5.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/hashes/MD5.d	Wed Aug 13 22:01:19 2008 -0400
@@ -210,6 +210,15 @@
         h3 = 0x10325476u;
     }
     
+    MD5 copy() {
+        MD5 h = new MD5(buffer[0..index]);
+        h.h0 = h0;
+        h.h1 = h1;
+        h.h2 = h2;
+        h.h3 = h3;
+        return h;
+    }
+    
     version (UnitTest) {
         // Found in Tango <3
         unittest {
@@ -238,7 +247,7 @@
                 h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i], 
-                        h.name()~": ("~digest~") != ("~test_results[i]~")");
+                        h.name~": ("~digest~") != ("~test_results[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/hashes/SHA1.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/hashes/SHA1.d	Wed Aug 13 22:01:19 2008 -0400
@@ -12,7 +12,7 @@
 
 /** Implementation of SHA-1. */
 class SHA1 : Hash {
-	private uint h0, h1, h2, h3, h4;
+	protected uint h0, h1, h2, h3, h4;
     
     this (void[] input_=null) {
         reset();
@@ -161,6 +161,16 @@
         h4 = 0xc3d2e1f0u;
     }
     
+    SHA1 copy() {
+        SHA1 h = new SHA1(buffer[0..index]);
+        h.h0 = h0;
+        h.h1 = h1;
+        h.h2 = h2;
+        h.h3 = h3;
+        h.h4 = h4;
+        return h;
+    }
+   
     version (UnitTest) {
         unittest {
             static const char[][] test_inputs = [
@@ -189,7 +199,7 @@
                     h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i], 
-                        h.name()~": ("~digest~") != ("~test_results[i]~")");
+                        h.name~": ("~digest~") != ("~test_results[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/hashes/SHA224.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/hashes/SHA224.d	Wed Aug 13 22:01:19 2008 -0400
@@ -37,7 +37,7 @@
         Util.uintToUbytesBig(h4, result, 16);
         Util.uintToUbytesBig(h5, result, 20);
         Util.uintToUbytesBig(h6, result, 24);
-
+        
         reset();
         return result;
     }
@@ -54,6 +54,19 @@
         h7 = 0xbefa4fa4u;
     }
     
+    SHA224 copy() {
+        SHA224 h = new SHA224(buffer[0..index]);
+        h.h0 = h0;
+        h.h1 = h1;
+        h.h2 = h2;
+        h.h3 = h3;
+        h.h4 = h4;
+        h.h5 = h5;
+        h.h6 = h6;
+        h.h7 = h7;
+        return h;
+    }
+    
     version (UnitTest) {
         unittest {
             static const char[][] test_inputs = [
@@ -80,7 +93,7 @@
                     h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i], 
-                        h.name()~": ("~digest~") != ("~test_results[i]~")");
+                        h.name~": ("~digest~") != ("~test_results[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/hashes/SHA256.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/hashes/SHA256.d	Wed Aug 13 22:01:19 2008 -0400
@@ -143,6 +143,19 @@
         h7 = 0x5be0cd19u;
     }
     
+    SHA256 copy() {
+        SHA256 h = new SHA256(buffer[0..index]);
+        h.h0 = h0;
+        h.h1 = h1;
+        h.h2 = h2;
+        h.h3 = h3;
+        h.h4 = h4;
+        h.h5 = h5;
+        h.h6 = h6;
+        h.h7 = h7;
+        return h;
+    }
+    
     version (UnitTest) {
         unittest {
             static const char[][] test_inputs = [
@@ -169,7 +182,7 @@
                     h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i], 
-                        h.name()~": ("~digest~") != ("~test_results[i]~")");
+                        h.name~": ("~digest~") != ("~test_results[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/hashes/SHA384.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/hashes/SHA384.d	Wed Aug 13 22:01:19 2008 -0400
@@ -52,6 +52,19 @@
         h7 = 0x47b5481dbefa4fa4u;
     }
     
+    SHA384 copy() {
+        SHA384 h = new SHA384(buffer[0..index]);
+        h.h0 = h0;
+        h.h1 = h1;
+        h.h2 = h2;
+        h.h3 = h3;
+        h.h4 = h4;
+        h.h5 = h5;
+        h.h6 = h6;
+        h.h7 = h7;
+        return h;
+    }
+    
     version (UnitTest) {
         unittest {
             static const char[][] test_inputs = [
@@ -86,7 +99,7 @@
                     h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i], 
-                        h.name()~": ("~digest~") != ("~test_results[i]~")");
+                        h.name~": ("~digest~") != ("~test_results[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/hashes/SHA512.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/hashes/SHA512.d	Wed Aug 13 22:01:19 2008 -0400
@@ -151,6 +151,19 @@
         h7 = 0x5be0cd19137e2179u;
     }
     
+    SHA512 copy() {
+        SHA512 h = new SHA512(buffer[0..index]);
+        h.h0 = h0;
+        h.h1 = h1;
+        h.h2 = h2;
+        h.h3 = h3;
+        h.h4 = h4;
+        h.h5 = h5;
+        h.h6 = h6;
+        h.h7 = h7;
+        return h;
+    }
+    
     version (UnitTest) {
         unittest {
             static const char[][] test_inputs = [
@@ -185,7 +198,7 @@
                     h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i], 
-                        h.name()~": ("~digest~") != ("~test_results[i]~")");
+                        h.name~": ("~digest~") != ("~test_results[i]~")");
             }
         }
     }
--- a/dcrypt/crypto/params/ParametersWithIV.d	Tue Aug 12 05:48:06 2008 -0400
+++ b/dcrypt/crypto/params/ParametersWithIV.d	Wed Aug 13 22:01:19 2008 -0400
@@ -20,7 +20,7 @@
      *     params = Parameters to wrap.
      *     iv     = IV to be held.
      */
-    this(CipherParameters params=null, ubyte[] iv_=null) {
+    this (CipherParameters params=null, ubyte[] iv_=null) {
         if (params)
             m_params = params;
         ubyte[] iv = cast(ubyte[]) iv_;