comparison dcrypt/crypto/prngs/PBKDF2.d @ 28:ad687db713a4

Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
author Thomas Dixon <reikon@reikon.us>
date Sun, 10 May 2009 22:38:48 -0400
parents 8b5eaf3c2979
children 21847420b1ac
comparison
equal deleted inserted replaced
27:8b5eaf3c2979 28:ad687db713a4
26 private 26 private
27 { 27 {
28 ubyte[] salt, 28 ubyte[] salt,
29 buffer; 29 buffer;
30 30
31 char[] password; 31 string password;
32 32
33 MAC prf; 33 MAC prf;
34 34
35 uint iterations, 35 uint iterations,
36 blockCount, 36 blockCount,
42 * password = User supplied password 42 * password = User supplied password
43 * salt = (preferably random) salt 43 * salt = (preferably random) salt
44 * iterations = The number of total iterations 44 * iterations = The number of total iterations
45 * prf = The pseudo-random function 45 * prf = The pseudo-random function
46 */ 46 */
47 this(char[] password, void[] salt_, uint iterations=1000, MAC prf=new HMAC(new SHA1)) 47 this(string password, void[] salt_, uint iterations=1000, MAC prf=new HMAC(new SHA1))
48 { 48 {
49 49
50 salt = cast(ubyte[])salt_; 50 salt = cast(ubyte[])salt_;
51 if (salt == null) 51 if (salt == null)
52 throw new InvalidParameterError(name()~": No salt specified."); 52 throw new InvalidParameterError(name()~": No salt specified.");
109 } 109 }
110 110
111 return output.length; 111 return output.length;
112 } 112 }
113 113
114 char[] name() 114 string name()
115 { 115 {
116 return "PBKDF2-"~prf.name; 116 return "PBKDF2-"~prf.name;
117 } 117 }
118 118
119 debug (UnitTest) 119 debug (UnitTest)
120 { 120 {
121 unittest 121 unittest
122 { 122 {
123 static char[][] test_passwords = [ 123 static string[] test_passwords = [
124 "password", 124 "password",
125 "password", 125 "password",
126 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"~ 126 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"~
127 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 127 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
128 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"~ 128 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"~
129 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 129 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
130 ]; 130 ];
131 131
132 static char[][] test_salts = [ 132 static string[] test_salts = [
133 "ATHENA.MIT.EDUraeburn", 133 "ATHENA.MIT.EDUraeburn",
134 "ATHENA.MIT.EDUraeburn", 134 "ATHENA.MIT.EDUraeburn",
135 "pass phrase equals block size", 135 "pass phrase equals block size",
136 "pass phrase exceeds block size" 136 "pass phrase exceeds block size"
137 ]; 137 ];
138 138
139 static const int[] test_iterations = [ 139 static const int[] test_iterations = [
140 1, 1200, 1200, 1200 140 1, 1200, 1200, 1200
141 ]; 141 ];
142 142
143 static const char[][] test_results = [ 143 static const string[] test_results = [
144 "cdedb5281bb2f801565a1122b2563515", 144 "cdedb5281bb2f801565a1122b2563515",
145 "5c08eb61fdf71e4e4ec3cf6ba1f5512b"~ 145 "5c08eb61fdf71e4e4ec3cf6ba1f5512b"~
146 "a7e52ddbc5e5142f708a31e2e62b1e13", 146 "a7e52ddbc5e5142f708a31e2e62b1e13",
147 "139c30c0966bc32ba55fdbf212530ac9"~ 147 "139c30c0966bc32ba55fdbf212530ac9"~
148 "c5ec59f1a452f5cc9ad940fea0598ed1", 148 "c5ec59f1a452f5cc9ad940fea0598ed1",
149 "9ccad6d468770cd51b10e6a68721be61"~ 149 "9ccad6d468770cd51b10e6a68721be61"~
150 "1a8b4d282601db3b36be9246915ec82a" 150 "1a8b4d282601db3b36be9246915ec82a"
151 ]; 151 ];
152 152
153 PBKDF2 pbkdf2; 153 PBKDF2 pbkdf2;
154 foreach (uint i, char[] p; test_passwords) 154 foreach (uint i, string p; test_passwords)
155 { 155 {
156 pbkdf2 = new PBKDF2(p, test_salts[i], test_iterations[i]); 156 pbkdf2 = new PBKDF2(p, test_salts[i], test_iterations[i]);
157 ubyte[] result = new ubyte[test_results[i].length >> 1]; 157 ubyte[] result = new ubyte[test_results[i].length >> 1];
158 pbkdf2.read(result); 158 pbkdf2.read(result);
159 char[] hexResult = ByteConverter.hexEncode(result); 159 string hexResult = ByteConverter.hexEncode(result);
160 assert(hexResult == test_results[i], 160 assert(hexResult == test_results[i],
161 pbkdf2.name~": ("~hexResult~") != ("~test_results[i]~")"); 161 pbkdf2.name~": ("~hexResult~") != ("~test_results[i]~")");
162 } 162 }
163 } 163 }
164 } 164 }