Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/RC6.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 |
---|---|
63 setup(workingKey); | 63 setup(workingKey); |
64 | 64 |
65 initialized = true; | 65 initialized = true; |
66 } | 66 } |
67 | 67 |
68 ubyte[] process(void[] input_) { | 68 uint update(void[] input_, void[] output_) { |
69 if (!initialized) | 69 if (!initialized) |
70 throw new NotInitializedError(name()~": Cipher not initialized"); | 70 throw new NotInitializedError(name()~": Cipher not initialized"); |
71 | 71 |
72 ubyte[] input = cast(ubyte[]) input_; | 72 ubyte[] input = cast(ubyte[]) input_, |
73 output = cast(ubyte[]) output_; | |
73 | 74 |
74 if (input.length > blockSize) | 75 if (input.length < BLOCK_SIZE) |
75 throw new ShortBufferError(name()~": Input buffer too short"); | 76 throw new ShortBufferError(name()~": Input buffer too short"); |
77 | |
78 if (output.length < BLOCK_SIZE) | |
79 throw new ShortBufferError(name()~": Output buffer too short"); | |
76 | 80 |
77 uint A = Util.ubytesToUintLittle(input, 0), | 81 uint A = Util.ubytesToUintLittle(input, 0), |
78 B = Util.ubytesToUintLittle(input, 4), | 82 B = Util.ubytesToUintLittle(input, 4), |
79 C = Util.ubytesToUintLittle(input, 8), | 83 C = Util.ubytesToUintLittle(input, 8), |
80 D = Util.ubytesToUintLittle(input, 12), | 84 D = Util.ubytesToUintLittle(input, 12), |
113 } | 117 } |
114 D -= S[1]; | 118 D -= S[1]; |
115 B -= S[0]; | 119 B -= S[0]; |
116 } | 120 } |
117 | 121 |
118 ubyte[] output = new ubyte[blockSize]; | |
119 Util.uintToUbytesLittle(A, output, 0); | 122 Util.uintToUbytesLittle(A, output, 0); |
120 Util.uintToUbytesLittle(B, output, 4); | 123 Util.uintToUbytesLittle(B, output, 4); |
121 Util.uintToUbytesLittle(C, output, 8); | 124 Util.uintToUbytesLittle(C, output, 8); |
122 Util.uintToUbytesLittle(D, output, 12); | 125 Util.uintToUbytesLittle(D, output, 12); |
123 | 126 |
124 return output; | 127 return BLOCK_SIZE; |
125 } | 128 } |
126 | 129 |
127 void reset() { | 130 void reset() { |
128 setup(workingKey); | 131 setup(workingKey); |
129 } | 132 } |
190 char[] result; | 193 char[] result; |
191 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); | 194 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); |
192 | 195 |
193 // Encryption | 196 // Encryption |
194 t.init(true, key); | 197 t.init(true, key); |
195 buffer = t.process(Util.hexToUbytes(test_plaintexts[i])); | 198 t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); |
196 result = Util.ubytesToHex(buffer); | 199 result = Util.ubytesToHex(buffer); |
197 assert(result == test_ciphertexts[i], | 200 assert(result == test_ciphertexts[i], |
198 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); | 201 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); |
199 | 202 |
200 // Decryption | 203 // Decryption |
201 t.init(false, key); | 204 t.init(false, key); |
202 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i])); | 205 t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer); |
203 result = Util.ubytesToHex(buffer); | 206 result = Util.ubytesToHex(buffer); |
204 assert(result == test_plaintexts[i], | 207 assert(result == test_plaintexts[i], |
205 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); | 208 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); |
206 } | 209 } |
207 } | 210 } |