Mercurial > projects > dcrypt
comparison dcrypt/crypto/macs/HMAC.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 | b9ba770b8f16 |
comparison
equal
deleted
inserted
replaced
27:8b5eaf3c2979 | 28:ad687db713a4 |
---|---|
81 throw new NotInitializedError(name()~": MAC not initialized."); | 81 throw new NotInitializedError(name()~": MAC not initialized."); |
82 | 82 |
83 hash.update(input_); | 83 hash.update(input_); |
84 } | 84 } |
85 | 85 |
86 char[] name() | 86 string name() |
87 { | 87 { |
88 return "HMAC-"~hash.name; | 88 return "HMAC-"~hash.name; |
89 } | 89 } |
90 | 90 |
91 void reset() | 91 void reset() |
114 reset(); | 114 reset(); |
115 | 115 |
116 return r; | 116 return r; |
117 } | 117 } |
118 | 118 |
119 char[] hexDigest() | 119 string hexDigest() |
120 { | 120 { |
121 return ByteConverter.hexEncode(digest()); | 121 return ByteConverter.hexEncode(digest()); |
122 } | 122 } |
123 | 123 |
124 HMAC copy() | 124 HMAC copy() |
132 | 132 |
133 debug (UnitTest) | 133 debug (UnitTest) |
134 { | 134 { |
135 unittest | 135 unittest |
136 { | 136 { |
137 static char[][] test_keys = [ | 137 static string[] test_keys = [ |
138 "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", | 138 "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", |
139 "4a656665", // Jefe? | 139 "4a656665", // Jefe? |
140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | 140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
141 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ | 141 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ |
142 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ | 142 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ |
143 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ | 143 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ |
144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | 144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
145 ]; | 145 ]; |
146 | 146 |
147 static char[][] test_inputs = [ | 147 static string[] test_inputs = [ |
148 "4869205468657265", | 148 "4869205468657265", |
149 "7768617420646f2079612077616e7420666f72206e6f7468696e673f", | 149 "7768617420646f2079612077616e7420666f72206e6f7468696e673f", |
150 "dd", | 150 "dd", |
151 "54657374205573696e67204c6172676572205468616e20426c6f63"~ | 151 "54657374205573696e67204c6172676572205468616e20426c6f63"~ |
152 "6b2d53697a65204b6579202d2048617368204b6579204669727374" | 152 "6b2d53697a65204b6579202d2048617368204b6579204669727374" |
154 | 154 |
155 static const int[] test_repeat = [ | 155 static const int[] test_repeat = [ |
156 1, 1, 50, 1 | 156 1, 1, 50, 1 |
157 ]; | 157 ]; |
158 | 158 |
159 static const char[][] test_results = [ | 159 static const string[] test_results = [ |
160 "b617318655057264e28bc0b6fb378c8ef146be00", | 160 "b617318655057264e28bc0b6fb378c8ef146be00", |
161 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", | 161 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", |
162 "125d7342b9ac11cd91a39af48aa17b4f63f175d3", | 162 "125d7342b9ac11cd91a39af48aa17b4f63f175d3", |
163 "aa4ae5e15272d00e95705637ce8a3b55ed402112" | 163 "aa4ae5e15272d00e95705637ce8a3b55ed402112" |
164 ]; | 164 ]; |
165 | 165 |
166 HMAC h = new HMAC(new SHA1()); | 166 HMAC h = new HMAC(new SHA1()); |
167 foreach (uint i, char[] k; test_keys) | 167 foreach (uint i, string k; test_keys) |
168 { | 168 { |
169 h.init(new SymmetricKey(ByteConverter.hexDecode(k))); | 169 h.init(new SymmetricKey(ByteConverter.hexDecode(k))); |
170 for (int j = 0; j < test_repeat[i]; j++) | 170 for (int j = 0; j < test_repeat[i]; j++) |
171 h.update(ByteConverter.hexDecode(test_inputs[i])); | 171 h.update(ByteConverter.hexDecode(test_inputs[i])); |
172 char[] mac = h.hexDigest(); | 172 string mac = h.hexDigest(); |
173 assert(mac == test_results[i], | 173 assert(mac == test_results[i], |
174 h.name~": ("~mac~") != ("~test_results[i]~")"); | 174 h.name~": ("~mac~") != ("~test_results[i]~")"); |
175 } | 175 } |
176 } | 176 } |
177 } | 177 } |