comparison 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
comparison
equal deleted inserted replaced
11:02970e63257d 12:8c7f8fecdd75
47 state[x] = state[y]; 47 state[x] = state[y];
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 ubyte[] process(void[] input_) { 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 = new ubyte[input.length]; 57 output = cast(ubyte[]) output_;
58
59 if (input.length > output.length)
60 throw new ShortBufferError(name()~": Output buffer too short");
58 61
59 for (int i = 0; i < input.length; i++) { 62 for (int i = 0; i < input.length; i++) {
60 y += state[++x]; 63 y += state[++x];
61 ubyte t = state[x]; 64 ubyte t = state[x];
62 state[x] = state[y]; 65 state[x] = state[y];
63 state[y] = t; 66 state[y] = t;
64 output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])]; 67 output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])];
65 } 68 }
66 return output; 69 return input.length;
67 } 70 }
68 71
69 void reset() { 72 void reset() {
70 setup(workingKey); 73 setup(workingKey);
71 } 74 }
180 char[] result; 183 char[] result;
181 184
182 r.init(true, new SymmetricKey(Util.hexToUbytes(test_key))); 185 r.init(true, new SymmetricKey(Util.hexToUbytes(test_key)));
183 186
184 // Encryption 187 // Encryption
185 buffer = r.process(Util.hexToUbytes(test_plaintexts[i])); 188 r.update(Util.hexToUbytes(test_plaintexts[i]), buffer);
186 result = Util.ubytesToHex(buffer); 189 result = Util.ubytesToHex(buffer);
187 assert(result == test_ciphertexts[i], 190 assert(result == test_ciphertexts[i],
188 r.name~": ("~result~") != ("~test_ciphertexts[i]~")"); 191 r.name~": ("~result~") != ("~test_ciphertexts[i]~")");
189 192
190 r.reset(); 193 r.reset();
191 194
192 // Decryption 195 // Decryption
193 buffer = r.process(Util.hexToUbytes(test_ciphertexts[i])); 196 r.update(Util.hexToUbytes(test_ciphertexts[i]), buffer);
194 result = Util.ubytesToHex(buffer); 197 result = Util.ubytesToHex(buffer);
195 assert(result == test_plaintexts[i], 198 assert(result == test_plaintexts[i],
196 r.name~": ("~result~") != ("~test_plaintexts[i]~")"); 199 r.name~": ("~result~") != ("~test_plaintexts[i]~")");
197 } 200 }
198 } 201 }