diff dcrypt/crypto/ciphers/Blowfish.d @ 12:8c7f8fecdd75

Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
author Thomas Dixon <reikon@reikon.us>
date Sat, 30 Aug 2008 14:38:23 -0400
parents 23c62e28b3a4
children 5ce3012f1def
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/Blowfish.d	Wed Aug 20 23:33:02 2008 -0400
+++ b/dcrypt/crypto/ciphers/Blowfish.d	Sat Aug 30 14:38:23 2008 -0400
@@ -255,14 +255,18 @@
                 + S3[cast(ubyte)x]);
     }
     
-    ubyte[] process(void[] input_) {
+    uint update(void[] input_, void[] output_) {
         if (!initialized)
             throw new NotInitializedError(name()~": Cipher not initialized.");
         
-        ubyte[] input = cast(ubyte[]) input_;
+        ubyte[] input = cast(ubyte[]) input_,
+                output = cast(ubyte[]) output_;
                     
-        if (input.length < blockSize)
+        if (input.length < BLOCK_SIZE)
             throw new ShortBufferError(name()~": Input buffer too short");
+            
+        if (output.length < BLOCK_SIZE)
+            throw new ShortBufferError(name()~": Output buffer too short");
         
         uint xl = Util.ubytesToUintBig(input, 0),
              xr = Util.ubytesToUintBig(input, 4),
@@ -275,11 +279,10 @@
         }
         xr ^= P[i];
   
-        ubyte[] output = new ubyte[blockSize];
         Util.uintToUbytesBig(xr, output, 0);
         Util.uintToUbytesBig(xl, output, 4);
         
-        return output;
+        return BLOCK_SIZE;
     }
     
     void reset() {
@@ -301,31 +304,31 @@
         
         ubyte[] t = new ubyte[BLOCK_SIZE]; // Initialized to 0's
         for (int i = 0; i < PBOX_SIZE;) {
-            t = process(t);
+            update(t, t);
             P[i++] = Util.ubytesToUintBig(t, 0);
             P[i++] = Util.ubytesToUintBig(t, 4);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
-            t = process(t);
+            update(t, t);
             S0[i++] = Util.ubytesToUintBig(t, 0);
             S0[i++] = Util.ubytesToUintBig(t, 4);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
-            t = process(t);
+            update(t, t);
             S1[i++] = Util.ubytesToUintBig(t, 0);
             S1[i++] = Util.ubytesToUintBig(t, 4);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
-            t = process(t);
+            update(t, t);
             S2[i++] = Util.ubytesToUintBig(t, 0);
             S2[i++] = Util.ubytesToUintBig(t, 4);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
-            t = process(t);
+            update(t, t);
             S3[i++] = Util.ubytesToUintBig(t, 0);
             S3[i++] = Util.ubytesToUintBig(t, 4);
         }
@@ -375,14 +378,14 @@
                 
                 // Encryption
                 t.init(true, key);
-                buffer = t.process(Util.hexToUbytes(test_plaintexts[i]));
+                t.update(Util.hexToUbytes(test_plaintexts[i]), buffer);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
                         t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             
                 // Decryption
                 t.init(false, key);
-                buffer = t.process(Util.hexToUbytes(test_ciphertexts[i]));
+                t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
                         t.name~": ("~result~") != ("~test_plaintexts[i]~")");