Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/Blowfish.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 | 3a57367afe34 |
comparison
equal
deleted
inserted
replaced
13:7ea528b61802 | 14:5ce3012f1def |
---|---|
207 MAX_KEY_SIZE = 56, // 448 bits | 207 MAX_KEY_SIZE = 56, // 448 bits |
208 MIN_KEY_SIZE = 4; // 32 bits | 208 MIN_KEY_SIZE = 4; // 32 bits |
209 uint[18] P; | 209 uint[18] P; |
210 uint[256] S0, S1, S2, S3; | 210 uint[256] S0, S1, S2, S3; |
211 ubyte[] workingKey; | 211 ubyte[] workingKey; |
212 bool initialized, | |
213 encrypt; | |
214 } // end private | 212 } // end private |
215 | 213 |
216 char[] name() { | 214 char[] name() { |
217 return "Blowfish"; | 215 return "Blowfish"; |
218 } | 216 } |
224 void init(bool encrypt, CipherParameters params) { | 222 void init(bool encrypt, CipherParameters params) { |
225 SymmetricKey keyParams = cast(SymmetricKey)params; | 223 SymmetricKey keyParams = cast(SymmetricKey)params; |
226 if (!keyParams) | 224 if (!keyParams) |
227 throw new InvalidParameterError( | 225 throw new InvalidParameterError( |
228 name()~": Invalid parameter passed to init"); | 226 name()~": Invalid parameter passed to init"); |
229 this.encrypt = encrypt; | 227 _encrypt = encrypt; |
230 | 228 |
231 uint len = keyParams.key.length; | 229 uint len = keyParams.key.length; |
232 if (len < MIN_KEY_SIZE || len > MAX_KEY_SIZE) | 230 if (len < MIN_KEY_SIZE || len > MAX_KEY_SIZE) |
233 throw new InvalidKeyError( | 231 throw new InvalidKeyError( |
234 name()~": Invalid key length (requires 4-56 bytes)"); | 232 name()~": Invalid key length (requires 4-56 bytes)"); |
239 S3[] = S_INIT[768..1024]; | 237 S3[] = S_INIT[768..1024]; |
240 P[] = P_INIT[]; | 238 P[] = P_INIT[]; |
241 | 239 |
242 workingKey = keyParams.key; | 240 workingKey = keyParams.key; |
243 // Yes, this is ghetto. I know. | 241 // Yes, this is ghetto. I know. |
244 initialized = true; | 242 _initialized = true; |
245 setup(workingKey); | 243 setup(workingKey); |
246 | 244 |
247 if (!encrypt) | 245 if (!_encrypt) |
248 P.reverse; // Oh yes I did. | 246 P.reverse; // Oh yes I did. |
249 } | 247 } |
250 | 248 |
251 private uint F(uint x) { | 249 private uint F(uint x) { |
252 return (((S0[(x >> 24)] | 250 return (((S0[(x >> 24)] |
254 ^ S2[cast(ubyte)(x >> 8)]) | 252 ^ S2[cast(ubyte)(x >> 8)]) |
255 + S3[cast(ubyte)x]); | 253 + S3[cast(ubyte)x]); |
256 } | 254 } |
257 | 255 |
258 uint update(void[] input_, void[] output_) { | 256 uint update(void[] input_, void[] output_) { |
259 if (!initialized) | 257 if (!_initialized) |
260 throw new NotInitializedError(name()~": Cipher not initialized."); | 258 throw new NotInitializedError(name()~": Cipher not initialized."); |
261 | 259 |
262 ubyte[] input = cast(ubyte[]) input_, | 260 ubyte[] input = cast(ubyte[]) input_, |
263 output = cast(ubyte[]) output_; | 261 output = cast(ubyte[]) output_; |
264 | 262 |