2 // HMACRIPEMD160Test.cs - NUnit Test Cases for HMACRIPEMD160
3 // http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using NUnit.Framework;
36 using System.Security.Cryptography;
39 namespace MonoTests.System.Security.Cryptography {
42 public class HMACRIPEMD160Test : KeyedHashAlgorithmTest {
44 protected HMACRIPEMD160 hmac;
46 // because most crypto stuff works with byte[] buffers
47 static public void AssertEquals (string msg, byte[] array1, byte[] array2)
49 if ((array1 == null) && (array2 == null))
52 Fail (msg + " -> First array is NULL");
54 Fail (msg + " -> Second array is NULL");
56 bool a = (array1.Length == array2.Length);
58 for (int i = 0; i < array1.Length; i++) {
59 if (array1 [i] != array2 [i]) {
65 if (array1.Length > 0) {
66 msg += " -> Expected " + BitConverter.ToString (array1, 0);
67 msg += " is different than " + BitConverter.ToString (array2, 0);
73 protected override void SetUp ()
75 hmac = new HMACRIPEMD160 ();
76 hmac.Key = new byte [8];
80 static byte[] key1 = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67 };
82 // HMACRIPEMD160 (key1, "") = cf387677bfda8483e63b57e06c3b5ecd8b7fc055
84 public void HMACRIPEMD160_Key1_Test1 ()
86 byte[] result = { 0xcf, 0x38, 0x76, 0x77, 0xbf, 0xda, 0x84, 0x83, 0xe6, 0x3b, 0x57, 0xe0, 0x6c, 0x3b, 0x5e, 0xcd, 0x8b, 0x7f, 0xc0, 0x55 };
87 byte[] input = new byte [0];
89 string testName = "HMACRIPEMD160 Key #1 Test #1";
91 HMACRIPEMD160_a (testName, hmac, input, result);
92 HMACRIPEMD160_b (testName, hmac, input, result);
93 HMACRIPEMD160_c (testName, hmac, input, result);
94 HMACRIPEMD160_d (testName, hmac, input, result);
95 // N/A RIPEMD160_e (testName, hmac, input, result);
98 // HMACRIPEMD160 ("a") = 0d351d71b78e36dbb7391c810a0d2b6240ddbafc
100 public void HMACRIPEMD160_Key1_Test2 ()
102 byte[] result = { 0x0d, 0x35, 0x1d, 0x71, 0xb7, 0x8e, 0x36, 0xdb, 0xb7, 0x39, 0x1c, 0x81, 0x0a, 0x0d, 0x2b, 0x62, 0x40, 0xdd, 0xba, 0xfc };
103 byte[] input = Encoding.Default.GetBytes ("a");
105 string testName = "HMACRIPEMD160 Key #1 Test #2";
107 HMACRIPEMD160_a (testName, hmac, input, result);
108 HMACRIPEMD160_b (testName, hmac, input, result);
109 HMACRIPEMD160_c (testName, hmac, input, result);
110 HMACRIPEMD160_d (testName, hmac, input, result);
111 HMACRIPEMD160_e (testName, hmac, input, result);
114 // HMACRIPEMD160 ("abc") = f7ef288cb1bbcc6160d76507e0a3bbf712fb67d6
116 public void HMACRIPEMD160_Key1_Test3 ()
118 byte[] result = { 0xf7, 0xef, 0x28, 0x8c, 0xb1, 0xbb, 0xcc, 0x61, 0x60, 0xd7, 0x65, 0x07, 0xe0, 0xa3, 0xbb, 0xf7, 0x12, 0xfb, 0x67, 0xd6 };
119 byte[] input = Encoding.Default.GetBytes ("abc");
121 string testName = "HMACRIPEMD160 Key #1 Test #3";
123 HMACRIPEMD160_a (testName, hmac, input, result);
124 HMACRIPEMD160_b (testName, hmac, input, result);
125 HMACRIPEMD160_c (testName, hmac, input, result);
126 HMACRIPEMD160_d (testName, hmac, input, result);
127 HMACRIPEMD160_e (testName, hmac, input, result);
130 // RIPEMD160 ("message digest") = f83662cc8d339c227e600fcd636c57d2571b1c34
132 public void HMACRIPEMD160_Key1_Test4 ()
134 byte[] result = { 0xf8, 0x36, 0x62, 0xcc, 0x8d, 0x33, 0x9c, 0x22, 0x7e, 0x60, 0x0f, 0xcd, 0x63, 0x6c, 0x57, 0xd2, 0x57, 0x1b, 0x1c, 0x34 };
135 byte[] input = Encoding.Default.GetBytes ("message digest");
137 string testName = "HMACRIPEMD160 Key #1 Test #4";
139 HMACRIPEMD160_a (testName, hmac, input, result);
140 HMACRIPEMD160_b (testName, hmac, input, result);
141 HMACRIPEMD160_c (testName, hmac, input, result);
142 HMACRIPEMD160_d (testName, hmac, input, result);
143 HMACRIPEMD160_e (testName, hmac, input, result);
146 // RIPEMD160 ("abcdefghijklmnopqrstuvwxyz") = 843d1c4eb880ac8ac0c9c95696507957d0155ddb
148 public void HMACRIPEMD160_Key1_Test5 ()
150 byte[] result = { 0x84, 0x3d, 0x1c, 0x4e, 0xb8, 0x80, 0xac, 0x8a, 0xc0, 0xc9, 0xc9, 0x56, 0x96, 0x50, 0x79, 0x57, 0xd0, 0x15, 0x5d, 0xdb };
151 byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
153 string testName = "HMACRIPEMD160 Key #1 Test #5";
155 HMACRIPEMD160_a (testName, hmac, input, result);
156 HMACRIPEMD160_b (testName, hmac, input, result);
157 HMACRIPEMD160_c (testName, hmac, input, result);
158 HMACRIPEMD160_d (testName, hmac, input, result);
159 HMACRIPEMD160_e (testName, hmac, input, result);
162 // RIPEMD160 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
163 // 60f5ef198a2dd5745545c1f0c47aa3fb5776f881
165 public void HMACRIPEMD160_Key1_Test6 ()
167 byte[] result = { 0x60, 0xf5, 0xef, 0x19, 0x8a, 0x2d, 0xd5, 0x74, 0x55, 0x45, 0xc1, 0xf0, 0xc4, 0x7a, 0xa3, 0xfb, 0x57, 0x76, 0xf8, 0x81 };
168 byte[] input = Encoding.Default.GetBytes ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
170 string testName = "HMACRIPEMD160 Key #1 Test #6";
172 HMACRIPEMD160_a (testName, hmac, input, result);
173 HMACRIPEMD160_b (testName, hmac, input, result);
174 HMACRIPEMD160_c (testName, hmac, input, result);
175 HMACRIPEMD160_d (testName, hmac, input, result);
176 HMACRIPEMD160_e (testName, hmac, input, result);
179 // RIPEMD160 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
180 // b0e20b6e3116640286ed3a87a5713079b21f5189
182 public void HMACRIPEMD160_Key1_Test7 ()
184 byte[] result = { 0xe4, 0x9c, 0x13, 0x6a, 0x9e, 0x56, 0x27, 0xe0, 0x68, 0x1b, 0x80, 0x8a, 0x3b, 0x97, 0xe6, 0xa6, 0xe6, 0x61, 0xae, 0x79 };
185 byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
187 string testName = "HMACRIPEMD160 Key #1 Test #7";
189 HMACRIPEMD160_a (testName, hmac, input, result);
190 HMACRIPEMD160_b (testName, hmac, input, result);
191 HMACRIPEMD160_c (testName, hmac, input, result);
192 HMACRIPEMD160_d (testName, hmac, input, result);
193 HMACRIPEMD160_e (testName, hmac, input, result);
196 // RIPEMD160 ("123456789012345678901234567890123456789012345678901234567890123456
197 // 78901234567890") = 9b752e45573d4b39f4dbd3323cab82bf63326bfb
199 public void HMACRIPEMD160_Key1_Test8 ()
201 byte[] result = { 0x31, 0xbe, 0x3c, 0xc9, 0x8c, 0xee, 0x37, 0xb7, 0x9b, 0x06, 0x19, 0xe3, 0xe1, 0xc2, 0xbe, 0x4f, 0x1a, 0xa5, 0x6e, 0x6c };
202 byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
204 string testName = "HMACRIPEMD160 Key #1 Test #8";
206 HMACRIPEMD160_a (testName, hmac, input, result);
207 HMACRIPEMD160_b (testName, hmac, input, result);
208 HMACRIPEMD160_c (testName, hmac, input, result);
209 HMACRIPEMD160_d (testName, hmac, input, result);
210 HMACRIPEMD160_e (testName, hmac, input, result);
213 // RIPEMD160 (1000000 x 'a') = 52783243c1697bdbe16d37f97f68f08325dc1528
215 public void HMACRIPEMD160_Key1_Test9 ()
217 byte[] result = { 0xc2, 0xaa, 0x88, 0xc6, 0x40, 0x56, 0x58, 0xdc, 0x22, 0x5e, 0x48, 0x54, 0x88, 0x37, 0x1f, 0xb2, 0x43, 0x3f, 0xa7, 0x35 };
218 byte[] input = new byte [1000000];
219 for (int i = 0; i < 1000000; i++)
220 input[i] = 0x61; // a
222 string testName = "HMACRIPEMD160 Key #1 Test #9";
224 HMACRIPEMD160_a (testName, hmac, input, result);
225 HMACRIPEMD160_b (testName, hmac, input, result);
226 HMACRIPEMD160_c (testName, hmac, input, result);
227 HMACRIPEMD160_d (testName, hmac, input, result);
228 HMACRIPEMD160_e (testName, hmac, input, result);
231 static byte[] key2 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33 };
233 // HMACRIPEMD160 (key2, "") = fe69a66c7423eea9c8fa2eff8d9dafb4f17a62f5
235 public void HMACRIPEMD160_Key2_Test1 ()
237 byte[] result = { 0xfe, 0x69, 0xa6, 0x6c, 0x74, 0x23, 0xee, 0xa9, 0xc8, 0xfa, 0x2e, 0xff, 0x8d, 0x9d, 0xaf, 0xb4, 0xf1, 0x7a, 0x62, 0xf5 };
238 byte[] input = new byte [0];
240 string testName = "HMACRIPEMD160 Key #2 Test #1";
242 HMACRIPEMD160_a (testName, hmac, input, result);
243 HMACRIPEMD160_b (testName, hmac, input, result);
244 HMACRIPEMD160_c (testName, hmac, input, result);
245 HMACRIPEMD160_d (testName, hmac, input, result);
246 // N/A RIPEMD160_e (testName, hmac, input, result);
249 // HMACRIPEMD160 ("a") = 85743e899bc82dbfa36faaa7a25b7cfd372432cd
251 public void HMACRIPEMD160_Key2_Test2 ()
253 byte[] result = { 0x85, 0x74, 0x3e, 0x89, 0x9b, 0xc8, 0x2d, 0xbf, 0xa3, 0x6f, 0xaa, 0xa7, 0xa2, 0x5b, 0x7c, 0xfd, 0x37, 0x24, 0x32, 0xcd };
254 byte[] input = Encoding.Default.GetBytes ("a");
256 string testName = "HMACRIPEMD160 Key #2 Test #2";
258 HMACRIPEMD160_a (testName, hmac, input, result);
259 HMACRIPEMD160_b (testName, hmac, input, result);
260 HMACRIPEMD160_c (testName, hmac, input, result);
261 HMACRIPEMD160_d (testName, hmac, input, result);
262 HMACRIPEMD160_e (testName, hmac, input, result);
265 // HMACRIPEMD160 ("abc") = 6e4afd501fa6b4a1823ca3b10bd9aa0ba97ba182
267 public void HMACRIPEMD160_Key2_Test3 ()
269 byte[] result = { 0x6e, 0x4a, 0xfd, 0x50, 0x1f, 0xa6, 0xb4, 0xa1, 0x82, 0x3c, 0xa3, 0xb1, 0x0b, 0xd9, 0xaa, 0x0b, 0xa9, 0x7b, 0xa1, 0x82 };
270 byte[] input = Encoding.Default.GetBytes ("abc");
272 string testName = "HMACRIPEMD160 Key #2 Test #3";
274 HMACRIPEMD160_a (testName, hmac, input, result);
275 HMACRIPEMD160_b (testName, hmac, input, result);
276 HMACRIPEMD160_c (testName, hmac, input, result);
277 HMACRIPEMD160_d (testName, hmac, input, result);
278 HMACRIPEMD160_e (testName, hmac, input, result);
281 // RIPEMD160 ("message digest") = 2e066e624badb76a184c8f90fba053330e650e92
283 public void HMACRIPEMD160_Key2_Test4 ()
285 byte[] result = { 0x2e, 0x06, 0x6e, 0x62, 0x4b, 0xad, 0xb7, 0x6a, 0x18, 0x4c, 0x8f, 0x90, 0xfb, 0xa0, 0x53, 0x33, 0x0e, 0x65, 0x0e, 0x92 };
286 byte[] input = Encoding.Default.GetBytes ("message digest");
288 string testName = "HMACRIPEMD160 Key #2 Test #4";
290 HMACRIPEMD160_a (testName, hmac, input, result);
291 HMACRIPEMD160_b (testName, hmac, input, result);
292 HMACRIPEMD160_c (testName, hmac, input, result);
293 HMACRIPEMD160_d (testName, hmac, input, result);
294 HMACRIPEMD160_e (testName, hmac, input, result);
297 // RIPEMD160 ("abcdefghijklmnopqrstuvwxyz") = 07e942aa4e3cd7c04dedc1d46e2e8cc4c741b3d9
299 public void HMACRIPEMD160_Key2_Test5 ()
301 byte[] result = { 0x07, 0xe9, 0x42, 0xaa, 0x4e, 0x3c, 0xd7, 0xc0, 0x4d, 0xed, 0xc1, 0xd4, 0x6e, 0x2e, 0x8c, 0xc4, 0xc7, 0x41, 0xb3, 0xd9 };
302 byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
304 string testName = "HMACRIPEMD160 Key #2 Test #5";
306 HMACRIPEMD160_a (testName, hmac, input, result);
307 HMACRIPEMD160_b (testName, hmac, input, result);
308 HMACRIPEMD160_c (testName, hmac, input, result);
309 HMACRIPEMD160_d (testName, hmac, input, result);
310 HMACRIPEMD160_e (testName, hmac, input, result);
313 // RIPEMD160 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
314 // b6582318ddcfb67a53a67d676b8ad869aded629a
316 public void HMACRIPEMD160_Key2_Test6 ()
318 byte[] result = { 0xb6, 0x58, 0x23, 0x18, 0xdd, 0xcf, 0xb6, 0x7a, 0x53, 0xa6, 0x7d, 0x67, 0x6b, 0x8a, 0xd8, 0x69, 0xad, 0xed, 0x62, 0x9a };
319 byte[] input = Encoding.Default.GetBytes ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
321 string testName = "HMACRIPEMD160 Key #2 Test #6";
323 HMACRIPEMD160_a (testName, hmac, input, result);
324 HMACRIPEMD160_b (testName, hmac, input, result);
325 HMACRIPEMD160_c (testName, hmac, input, result);
326 HMACRIPEMD160_d (testName, hmac, input, result);
327 HMACRIPEMD160_e (testName, hmac, input, result);
330 // RIPEMD160 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
331 // f1be3ee877703140d34f97ea1ab3a07c141333e2
333 public void HMACRIPEMD160_Key2_Test7 ()
335 byte[] result = { 0xf1, 0xbe, 0x3e, 0xe8, 0x77, 0x70, 0x31, 0x40, 0xd3, 0x4f, 0x97, 0xea, 0x1a, 0xb3, 0xa0, 0x7c, 0x14, 0x13, 0x33, 0xe2 };
336 byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
338 string testName = "HMACRIPEMD160 Key #2 Test #7";
340 HMACRIPEMD160_a (testName, hmac, input, result);
341 HMACRIPEMD160_b (testName, hmac, input, result);
342 HMACRIPEMD160_c (testName, hmac, input, result);
343 HMACRIPEMD160_d (testName, hmac, input, result);
344 HMACRIPEMD160_e (testName, hmac, input, result);
347 // RIPEMD160 ("123456789012345678901234567890123456789012345678901234567890123456
348 // 78901234567890") = 85f164703e61a63131be7e45958e0794123904f9
350 public void HMACRIPEMD160_Key2_Test8 ()
352 byte[] result = { 0x85, 0xf1, 0x64, 0x70, 0x3e, 0x61, 0xa6, 0x31, 0x31, 0xbe, 0x7e, 0x45, 0x95, 0x8e, 0x07, 0x94, 0x12, 0x39, 0x04, 0xf9 };
353 byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
355 string testName = "HMACRIPEMD160 Key #2 Test #8";
357 HMACRIPEMD160_a (testName, hmac, input, result);
358 HMACRIPEMD160_b (testName, hmac, input, result);
359 HMACRIPEMD160_c (testName, hmac, input, result);
360 HMACRIPEMD160_d (testName, hmac, input, result);
361 HMACRIPEMD160_e (testName, hmac, input, result);
364 // RIPEMD160 (1000000 x 'a') = 82a504a002ba6e6c67f3cd67cedb66dc169bab7a
366 public void HMACRIPEMD160_Key2_Test9 ()
368 byte[] result = { 0x82, 0xa5, 0x04, 0xa0, 0x02, 0xba, 0x6e, 0x6c, 0x67, 0xf3, 0xcd, 0x67, 0xce, 0xdb, 0x66, 0xdc, 0x16, 0x9b, 0xab, 0x7a };
369 byte[] input = new byte [1000000];
370 for (int i = 0; i < 1000000; i++)
371 input[i] = 0x61; // a
373 string testName = "HMACRIPEMD160 Key #2 Test #9";
375 HMACRIPEMD160_a (testName, hmac, input, result);
376 HMACRIPEMD160_b (testName, hmac, input, result);
377 HMACRIPEMD160_c (testName, hmac, input, result);
378 HMACRIPEMD160_d (testName, hmac, input, result);
379 HMACRIPEMD160_e (testName, hmac, input, result);
382 public void HMACRIPEMD160_a (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result)
384 byte[] output = hmac.ComputeHash (input);
385 AssertEquals (testName + ".a.1", result, output);
386 AssertEquals (testName + ".a.2", result, hmac.Hash);
387 // required or next operation will still return old hash
391 public void HMACRIPEMD160_b (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result)
393 byte[] output = hmac.ComputeHash (input, 0, input.Length);
394 AssertEquals (testName + ".b.1", result, output);
395 AssertEquals (testName + ".b.2", result, hmac.Hash);
396 // required or next operation will still return old hash
400 public void HMACRIPEMD160_c (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result)
402 MemoryStream ms = new MemoryStream (input);
403 byte[] output = hmac.ComputeHash (ms);
404 AssertEquals (testName + ".c.1", result, output);
405 AssertEquals (testName + ".c.2", result, hmac.Hash);
406 // required or next operation will still return old hash
410 public void HMACRIPEMD160_d (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result)
412 hmac.TransformFinalBlock (input, 0, input.Length);
413 AssertEquals (testName + ".d", result, hmac.Hash);
414 // required or next operation will still return old hash
418 public void HMACRIPEMD160_e (string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result)
420 byte[] copy = new byte [input.Length];
421 for (int i=0; i < input.Length - 1; i++)
422 hmac.TransformBlock (input, i, 1, copy, i);
423 hmac.TransformFinalBlock (input, input.Length - 1, 1);
424 AssertEquals (testName + ".e", result, hmac.Hash);
425 // required or next operation will still return old hash
429 // none of those values changes for any implementation of RIPEMD160
431 public virtual void StaticInfo ()
433 string className = hmac.ToString ();
434 AssertEquals (className + ".HashSize", 160, hmac.HashSize);
435 AssertEquals (className + ".InputBlockSize", 1, hmac.InputBlockSize);
436 AssertEquals (className + ".OutputBlockSize", 1, hmac.OutputBlockSize);