diff dcrypt/crypto/ciphers/RC4.d @ 8:23c62e28b3a4

Reworked symmetric cipher classes to have SymmetricCipher as their superclass, and follow the general interface of init(), process(), etc. Made sure everything still passed test vectors. Removed Cipher class. I'll worry about that shit when we support something other than symmetric ciphers.
author Thomas Dixon <reikon@reikon.us>
date Mon, 18 Aug 2008 01:14:37 -0400
parents 5cb17e09d685
children 8c7f8fecdd75
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/RC4.d	Sat Aug 16 22:55:38 2008 -0400
+++ b/dcrypt/crypto/ciphers/RC4.d	Mon Aug 18 01:14:37 2008 -0400
@@ -49,26 +49,21 @@
         return (input^state[cast(ubyte)(state[x]+state[y])]);
     }
     
-    void processBytes(void[] input_, uint inOff, uint len, void[] output_, uint outOff) {
+    ubyte[] process(void[] input_) {
         if (!initialized)
             throw new NotInitializedError(name()~": Cipher not initialized");
             
-        ubyte[] input = cast(ubyte[]) input_;
-        ubyte[] output = cast(ubyte[]) output_;
-        
-        if ((inOff + len) > input.length)
-            throw new ShortBufferError(name()~": Input buffer too short");
+        ubyte[] input = cast(ubyte[]) input_,
+                output = new ubyte[input.length];
             
-        if ((outOff + len) > output.length)
-            throw new ShortBufferError(name()~": Output buffer too short");
-            
-        for (int i = 0; i < len; i++) {
+        for (int i = 0; i < input.length; i++) {
             y += state[++x];
             ubyte t = state[x];
             state[x] = state[y];
             state[y] = t;
-            output[outOff++] = input[inOff++] ^ state[cast(ubyte)(state[x]+state[y])];
+            output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])];
         }
+        return output;
     }
     
     void reset() { 
@@ -187,7 +182,7 @@
                 r.init(true, new SymmetricKey(Util.hexToUbytes(test_key)));
                 
                 // Encryption
-                r.processBytes(Util.hexToUbytes(test_plaintexts[i]), 0, buffer.length, buffer, 0);
+                buffer = r.process(Util.hexToUbytes(test_plaintexts[i]));
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
                         r.name~": ("~result~") != ("~test_ciphertexts[i]~")");
@@ -195,7 +190,7 @@
                 r.reset();
                 
                 // Decryption
-                r.processBytes(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer.length, buffer, 0);
+                buffer = r.process(Util.hexToUbytes(test_ciphertexts[i]));
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
                         r.name~": ("~result~") != ("~test_plaintexts[i]~")");