diff dcrypt/crypto/ciphers/RC4.d @ 24:6428dfd7fede

Implemented Salsa20. Added some error checking to RC4's returnByte. Fixed copyright year in Bitwise.d and ByteConverter.d. Added Robert Smith to contributors file. Thanks buddy :)
author Thomas Dixon <reikon@reikon.us>
date Thu, 19 Feb 2009 14:45:13 -0500
parents 4589f8c5eb3c
children 176c933827a8
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/RC4.d	Sat Feb 14 19:58:20 2009 -0500
+++ b/dcrypt/crypto/ciphers/RC4.d	Thu Feb 19 14:45:13 2009 -0500
@@ -1,7 +1,7 @@
 /**
  * This file is part of the dcrypt project.
  *
- * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved.
+ * Copyright: Copyright (C) dcrypt contributors 2009. All rights reserved.
  * License:   MIT
  * Authors:   Thomas Dixon
  */
@@ -22,19 +22,25 @@
         ubyte x, y;
     }
     
+    this() {
+        state = new ubyte[256];
+    }
+    
     void init(bool encrypt, CipherParameters params) {
         SymmetricKey keyParams = cast(SymmetricKey)params;
+        
         if (!keyParams)
             throw new InvalidParameterError(
                     name()~": Invalid parameter passed to init");
+                    
         if (keyParams.key.length < 0 || keyParams.key.length > 256)
             throw new InvalidKeyError(
                 name()~": Invalid key length (requires 1-256 bytes)");
+                
         workingKey = keyParams.key;
-        state = new ubyte[256];
         setup(workingKey);
-        _encrypt = true;
-        _initialized = true;
+        
+        _encrypt = _initialized = true;
     }
     
     char[] name() {
@@ -42,10 +48,14 @@
     }
     
     ubyte returnByte(ubyte input) {
+        if (!_initialized)
+            throw new NotInitializedError(name()~": Cipher not initialized");
+            
         y += state[++x];
         ubyte t = state[x];
         state[x] = state[y];
         state[y] = t;
+        
         return (input^state[cast(ubyte)(state[x]+state[y])]);
     }
     
@@ -66,6 +76,7 @@
             state[y] = t;
             output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])];
         }
+        
         return input.length;
     }
     
@@ -85,6 +96,7 @@
             state[i] = state[x];
             state[x] = t;
         }
+        
         x = y = 0;
     }