comparison dcrypt/crypto/padding/RFC1321.d @ 27:8b5eaf3c2979

Fixed error in hash message padding reported by Glenn Haecker.
author Thomas Dixon <reikon@reikon.us>
date Sat, 09 May 2009 23:29:20 -0400
parents cd376996cdb3
children ad687db713a4
comparison
equal deleted inserted replaced
26:176c933827a8 27:8b5eaf3c2979
12 12
13 /** 13 /**
14 * This class implements the padding described in RFC1321 (MD5 spec). 14 * This class implements the padding described in RFC1321 (MD5 spec).
15 * Ex. [... 0x80, 0x00 ... 0x00] 15 * Ex. [... 0x80, 0x00 ... 0x00]
16 */ 16 */
17 class RFC1321 : BlockCipherPadding { 17 class RFC1321 : BlockCipherPadding
18 char[] name() { 18 {
19 char[] name()
20 {
19 return "RFC1321"; 21 return "RFC1321";
20 } 22 }
21 23
22 ubyte[] pad(uint len) { 24 ubyte[] pad(uint len)
25 {
23 ubyte[] output = new ubyte[len]; 26 ubyte[] output = new ubyte[len];
24 27
25 output[0] = 0x80; 28 output[0] = 0x80;
26 output[1..output.length] = 0; 29 output[1..output.length] = 0;
27 30
28 return output; 31 return output;
29 } 32 }
30 33
31 uint unpad(void[] input_) { 34 uint unpad(void[] input_)
35 {
32 ubyte[] input = cast(ubyte[]) input_; 36 ubyte[] input = cast(ubyte[]) input_;
33 37
34 uint len = input.length; 38 uint len = input.length;
35 39
36 while (len-- > 0) 40 while (len-- > 0)
37 if (input[len] != 0) break; 41 if (input[len] != 0) break;
38 42
39 if (input[len] != 0x80) 43 if (input[len] != 0x80)
40 throw new InvalidPaddingError( 44 throw new InvalidPaddingError(name()~": Incorrect padding.");
41 name()~": Incorrect padding.");
42 45
43 return (input.length - len); 46 return (input.length - len);
44 } 47 }
45 } 48 }