comparison dcrypt/crypto/ciphers/RC4.d @ 14:5ce3012f1def

Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
author Thomas Dixon <reikon@reikon.us>
date Tue, 18 Nov 2008 18:03:40 -0500
parents 8c7f8fecdd75
children 4589f8c5eb3c
comparison
equal deleted inserted replaced
13:7ea528b61802 14:5ce3012f1def
18 class RC4 : StreamCipher { 18 class RC4 : StreamCipher {
19 private { 19 private {
20 ubyte[] state, 20 ubyte[] state,
21 workingKey; 21 workingKey;
22 ubyte x, y; 22 ubyte x, y;
23 bool initialized;
24 } 23 }
25 24
26 void init(bool encrypt, CipherParameters params) { 25 void init(bool encrypt, CipherParameters params) {
27 SymmetricKey keyParams = cast(SymmetricKey)params; 26 SymmetricKey keyParams = cast(SymmetricKey)params;
28 if (!keyParams) 27 if (!keyParams)
32 throw new InvalidKeyError( 31 throw new InvalidKeyError(
33 name()~": Invalid key length (requires 1-256 bytes)"); 32 name()~": Invalid key length (requires 1-256 bytes)");
34 workingKey = keyParams.key; 33 workingKey = keyParams.key;
35 state = new ubyte[256]; 34 state = new ubyte[256];
36 setup(workingKey); 35 setup(workingKey);
37 initialized = true; 36 _encrypt = true;
37 _initialized = true;
38 } 38 }
39 39
40 char[] name() { 40 char[] name() {
41 return "RC4"; 41 return "RC4";
42 } 42 }
48 state[y] = t; 48 state[y] = t;
49 return (input^state[cast(ubyte)(state[x]+state[y])]); 49 return (input^state[cast(ubyte)(state[x]+state[y])]);
50 } 50 }
51 51
52 uint update(void[] input_, void[] output_) { 52 uint update(void[] input_, void[] output_) {
53 if (!initialized) 53 if (!_initialized)
54 throw new NotInitializedError(name()~": Cipher not initialized"); 54 throw new NotInitializedError(name()~": Cipher not initialized");
55 55
56 ubyte[] input = cast(ubyte[]) input_, 56 ubyte[] input = cast(ubyte[]) input_,
57 output = cast(ubyte[]) output_; 57 output = cast(ubyte[]) output_;
58 58