diff dcrypt/crypto/ciphers/RC4.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/RC4.d	Wed Aug 20 23:33:02 2008 -0400
+++ b/dcrypt/crypto/ciphers/RC4.d	Sat Aug 30 14:38:23 2008 -0400
@@ -49,12 +49,15 @@
         return (input^state[cast(ubyte)(state[x]+state[y])]);
     }
     
-    ubyte[] process(void[] input_) {
+    uint update(void[] input_, void[] output_) {
         if (!initialized)
             throw new NotInitializedError(name()~": Cipher not initialized");
             
         ubyte[] input = cast(ubyte[]) input_,
-                output = new ubyte[input.length];
+                output = cast(ubyte[]) output_;
+            
+        if (input.length > output.length)
+            throw new ShortBufferError(name()~": Output buffer too short");
             
         for (int i = 0; i < input.length; i++) {
             y += state[++x];
@@ -63,7 +66,7 @@
             state[y] = t;
             output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])];
         }
-        return output;
+        return input.length;
     }
     
     void reset() { 
@@ -182,7 +185,7 @@
                 r.init(true, new SymmetricKey(Util.hexToUbytes(test_key)));
                 
                 // Encryption
-                buffer = r.process(Util.hexToUbytes(test_plaintexts[i]));
+                r.update(Util.hexToUbytes(test_plaintexts[i]), buffer);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
                         r.name~": ("~result~") != ("~test_ciphertexts[i]~")");
@@ -190,7 +193,7 @@
                 r.reset();
                 
                 // Decryption
-                buffer = r.process(Util.hexToUbytes(test_ciphertexts[i]));
+                r.update(Util.hexToUbytes(test_ciphertexts[i]), buffer);
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
                         r.name~": ("~result~") != ("~test_plaintexts[i]~")");