This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RIPEMD160Test.cs
1 //
2 // RIPEMD160Test.cs - NUnit Test Cases for RIPEMD160
3 //      http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
4 //
5 // Author:
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 //
10
11 #if NET_2_0
12
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.Security.Cryptography;
17 using System.Text;
18
19 namespace MonoTests.System.Security.Cryptography {
20
21         // RIPEMD160 is a abstract class - so ALL of the test included here wont be tested
22         // on the abstract class but should be tested in ALL its descendants.
23         public class RIPEMD160Test {
24
25                 protected RIPEMD160 hash;
26
27                 // because most crypto stuff works with byte[] buffers
28                 static public void AssertEquals (string msg, byte[] array1, byte[] array2) 
29                 {
30                         if ((array1 == null) && (array2 == null))
31                                 return;
32                         if (array1 == null)
33                                 TestCase.Fail (msg + " -> First array is NULL");
34                         if (array2 == null)
35                                 TestCase.Fail (msg + " -> Second array is NULL");
36                 
37                         bool a = (array1.Length == array2.Length);
38                         if (a) {
39                                 for (int i = 0; i < array1.Length; i++) {
40                                         if (array1 [i] != array2 [i]) {
41                                                 a = false;
42                                                 break;
43                                         }
44                                 }
45                         }
46                         if (array1.Length > 0) {
47                                 msg += " -> Expected " + BitConverter.ToString (array1, 0);
48                                 msg += " is different than " + BitConverter.ToString (array2, 0);
49                         }
50                         TestCase.Assert (msg, a);
51                 }
52
53                 // RIPEMD160 ("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31
54                 [Test]
55                 public void RIPEMD160_Test1 () 
56                 {
57                         string className = hash.ToString ();
58                         byte[] result = { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 };
59                         byte[] input = new byte [0];
60                 
61                         string testName = className + " 1";
62                         RIPEMD160_a (testName, hash, input, result);
63                         RIPEMD160_b (testName, hash, input, result);
64                         RIPEMD160_c (testName, hash, input, result);
65                         RIPEMD160_d (testName, hash, input, result);
66                         // N/A RIPEMD160_e (testName, hash, input, result);
67                 }
68
69                 // RIPEMD160 ("a") = 0bdc9d2d256b3ee9daae347be6f4dc835a467ffe
70                 [Test]
71                 public void RIPEMD160_Test2 () 
72                 {
73                         string className = hash.ToString ();
74                         byte[] result = { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae, 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe };
75                         byte[] input = Encoding.Default.GetBytes ("a");
76                 
77                         string testName = className + " 2";
78                         RIPEMD160_a (testName, hash, input, result);
79                         RIPEMD160_b (testName, hash, input, result);
80                         RIPEMD160_c (testName, hash, input, result);
81                         RIPEMD160_d (testName, hash, input, result);
82                         RIPEMD160_e (testName, hash, input, result);
83                 }
84
85                 // RIPEMD160 ("abc") = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
86                 [Test]
87                 public void RIPEMD160_Test3 () 
88                 {
89                         string className = hash.ToString ();
90                         byte[] result = { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04, 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc };
91                         byte[] input = Encoding.Default.GetBytes ("abc");
92                 
93                         string testName = className + " 3";
94                         RIPEMD160_a (testName, hash, input, result);
95                         RIPEMD160_b (testName, hash, input, result);
96                         RIPEMD160_c (testName, hash, input, result);
97                         RIPEMD160_d (testName, hash, input, result);
98                         RIPEMD160_e (testName, hash, input, result);
99                 }
100
101                 // RIPEMD160 ("message digest") = 5d0689ef49d2fae572b881b123a85ffa21595f36
102                 [Test]
103                 public void RIPEMD160_Test4 () 
104                 {
105                         string className = hash.ToString ();
106                         byte[] result = { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8, 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 };
107                         byte[] input = Encoding.Default.GetBytes ("message digest");
108                 
109                         string testName = className + " 4";
110                         RIPEMD160_a (testName, hash, input, result);
111                         RIPEMD160_b (testName, hash, input, result);
112                         RIPEMD160_c (testName, hash, input, result);
113                         RIPEMD160_d (testName, hash, input, result);
114                         RIPEMD160_e (testName, hash, input, result);
115                 }
116
117                 // RIPEMD160 ("abcdefghijklmnopqrstuvwxyz") = f71c27109c692c1b56bbdceb5b9d2865b3708dbc
118                 [Test]
119                 public void RIPEMD160_Test5 () 
120                 {
121                         string className = hash.ToString ();
122                         byte[] result = { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb, 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc };
123                         byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
124                 
125                         string testName = className + " 5";
126                         RIPEMD160_a (testName, hash, input, result);
127                         RIPEMD160_b (testName, hash, input, result);
128                         RIPEMD160_c (testName, hash, input, result);
129                         RIPEMD160_d (testName, hash, input, result);
130                         RIPEMD160_e (testName, hash, input, result);
131                 }
132
133                 // RIPEMD160 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
134                 //      12a053384a9c0c88e405a06c27dcf49ada62eb2b
135                 [Test]
136                 public void RIPEMD160_Test6 () 
137                 {
138                         string className = hash.ToString ();
139                         byte[] result = { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b };
140                         byte[] input = Encoding.Default.GetBytes ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
141                 
142                         string testName = className + " 6";
143                         RIPEMD160_a (testName, hash, input, result);
144                         RIPEMD160_b (testName, hash, input, result);
145                         RIPEMD160_c (testName, hash, input, result);
146                         RIPEMD160_d (testName, hash, input, result);
147                         RIPEMD160_e (testName, hash, input, result);
148                 }
149
150                 // RIPEMD160 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
151                 //      b0e20b6e3116640286ed3a87a5713079b21f5189
152                 [Test]
153                 public void RIPEMD160_Test7 () 
154                 {
155                         string className = hash.ToString ();
156                         byte[] result = { 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed, 0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89 };
157                         byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
158                 
159                         string testName = className + " 6";
160                         RIPEMD160_a (testName, hash, input, result);
161                         RIPEMD160_b (testName, hash, input, result);
162                         RIPEMD160_c (testName, hash, input, result);
163                         RIPEMD160_d (testName, hash, input, result);
164                         RIPEMD160_e (testName, hash, input, result);
165                 }
166
167                 // RIPEMD160 ("123456789012345678901234567890123456789012345678901234567890123456
168                 //      78901234567890") = 9b752e45573d4b39f4dbd3323cab82bf63326bfb
169                 [Test]
170                 public void RIPEMD160_Test8 () 
171                 {
172                         string className = hash.ToString ();
173                         byte[] result = { 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb, 0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb };
174                         byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
175                 
176                         string testName = className + " 7";
177                         RIPEMD160_a (testName, hash, input, result);
178                         RIPEMD160_b (testName, hash, input, result);
179                         RIPEMD160_c (testName, hash, input, result);
180                         RIPEMD160_d (testName, hash, input, result);
181                         RIPEMD160_e (testName, hash, input, result);
182                 }
183
184                 // RIPEMD160 (1000000 x 'a') = 52783243c1697bdbe16d37f97f68f08325dc1528
185                 [Test]
186                 public void RIPEMD160_Test9 () 
187                 {
188                         string className = hash.ToString ();
189                         byte[] result = { 0x52, 0x78, 0x32, 0x43, 0xc1, 0x69, 0x7b, 0xdb, 0xe1, 0x6d, 0x37, 0xf9, 0x7f, 0x68, 0xf0, 0x83, 0x25, 0xdc, 0x15, 0x28 };
190                         byte[] input = new byte [1000000];
191                         for (int i = 0; i < 1000000; i++)
192                                 input[i] = 0x61; // a
193                 
194                         string testName = className + " 7";
195                         RIPEMD160_a (testName, hash, input, result);
196                         RIPEMD160_b (testName, hash, input, result);
197                         RIPEMD160_c (testName, hash, input, result);
198                         RIPEMD160_d (testName, hash, input, result);
199                         RIPEMD160_e (testName, hash, input, result);
200                 }
201
202                 public void RIPEMD160_a (string testName, RIPEMD160 hash, byte[] input, byte[] result) 
203                 {
204                         byte[] output = hash.ComputeHash (input); 
205                         AssertEquals (testName + ".a.1", result, output);
206                         AssertEquals (testName + ".a.2", result, hash.Hash);
207                         // required or next operation will still return old hash
208                         hash.Initialize ();
209                 }
210
211                 public void RIPEMD160_b (string testName, RIPEMD160 hash, byte[] input, byte[] result) 
212                 {
213                         byte[] output = hash.ComputeHash (input, 0, input.Length); 
214                         AssertEquals (testName + ".b.1", result, output);
215                         AssertEquals (testName + ".b.2", result, hash.Hash);
216                         // required or next operation will still return old hash
217                         hash.Initialize ();
218                 }
219
220                 public void RIPEMD160_c (string testName, RIPEMD160 hash, byte[] input, byte[] result) 
221                 {
222                         MemoryStream ms = new MemoryStream (input);
223                         byte[] output = hash.ComputeHash (ms); 
224                         AssertEquals (testName + ".c.1", result, output);
225                         AssertEquals (testName + ".c.2", result, hash.Hash);
226                         // required or next operation will still return old hash
227                         hash.Initialize ();
228                 }
229
230                 public void RIPEMD160_d (string testName, RIPEMD160 hash, byte[] input, byte[] result) 
231                 {
232                         byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
233                         AssertEquals (testName + ".d.1", input, output);
234                         AssertEquals (testName + ".d.2", result, hash.Hash);
235                         // required or next operation will still return old hash
236                         hash.Initialize ();
237                 }
238
239                 public void RIPEMD160_e (string testName, RIPEMD160 hash, byte[] input, byte[] result) 
240                 {
241                         byte[] copy = new byte [input.Length];
242                         for (int i=0; i < input.Length - 1; i++)
243                                 hash.TransformBlock (input, i, 1, copy, i);
244                         byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
245                         TestCase.AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
246                         AssertEquals (testName + ".e.2", result, hash.Hash);
247                         // required or next operation will still return old hash
248                         hash.Initialize ();
249                 }
250
251                 // none of those values changes for any implementation of RIPEMD160
252                 public virtual void StaticInfo () 
253                 {
254                         string className = hash.ToString ();
255                         TestCase.AssertEquals (className + ".HashSize", 160, hash.HashSize);
256                         TestCase.AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
257                         TestCase.AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
258                 }
259         }
260 }
261
262 #endif