**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Security.Win32 / Test / Mono.Security.Cryptography / MD5Test.cs
blob1eb7078938063058c27009e421bf3d59ca4c88fc
1 //
2 // MD5Test.cs - NUnit Test Cases for MD5 (RFC1321)
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
10 using System;
11 using System.IO;
12 using System.Security.Cryptography;
13 using System.Text;
15 using Mono.Security.Cryptography;
16 using NUnit.Framework;
18 namespace MonoTests.Security.Cryptography {
20 // References:
21 // a. The MD5 Message-Digest Algorithm
22 // http://www.ietf.org/rfc/RFC1321.txt
24 // MD5 is a abstract class - so ALL of the test included here wont be tested
25 // on the abstract class but should be tested in ALL its descendants.
26 public class MD5Test {
28 protected MD5 hash;
30 // because most crypto stuff works with byte[] buffers
31 static public void AssertEquals (string msg, byte[] array1, byte[] array2)
33 if ((array1 == null) && (array2 == null))
34 return;
35 if (array1 == null)
36 Assertion.Fail (msg + " -> First array is NULL");
37 if (array2 == null)
38 Assertion.Fail (msg + " -> Second array is NULL");
40 bool a = (array1.Length == array2.Length);
41 if (a) {
42 for (int i = 0; i < array1.Length; i++) {
43 if (array1 [i] != array2 [i]) {
44 a = false;
45 break;
49 if (array1.Length > 0) {
50 msg += " -> Expected " + BitConverter.ToString (array1, 0);
51 msg += " is different than " + BitConverter.ToString (array2, 0);
53 Assertion.Assert (msg, a);
56 // MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
57 [Test]
58 public void RFC1321_Test1 ()
60 string className = hash.ToString ();
61 byte[] result = { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e };
62 byte[] input = new byte [0];
64 string testName = className + " 1";
65 RFC1321_a (testName, hash, input, result);
66 RFC1321_b (testName, hash, input, result);
67 RFC1321_c (testName, hash, input, result);
68 RFC1321_d (testName, hash, input, result);
69 // N/A RFC1321_e (testName, hash, input, result);
72 // MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
73 [Test]
74 public void RFC1321_Test2 ()
76 string className = hash.ToString ();
77 byte[] result = { 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 };
78 byte[] input = Encoding.Default.GetBytes ("a");
80 string testName = className + " 2";
81 RFC1321_a (testName, hash, input, result);
82 RFC1321_b (testName, hash, input, result);
83 RFC1321_c (testName, hash, input, result);
84 RFC1321_d (testName, hash, input, result);
85 RFC1321_e (testName, hash, input, result);
88 // MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
89 [Test]
90 public void RFC1321_Test3 ()
92 string className = hash.ToString ();
93 byte[] result = { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 };
94 byte[] input = Encoding.Default.GetBytes ("abc");
96 string testName = className + " 3";
97 RFC1321_a (testName, hash, input, result);
98 RFC1321_b (testName, hash, input, result);
99 RFC1321_c (testName, hash, input, result);
100 RFC1321_d (testName, hash, input, result);
101 RFC1321_e (testName, hash, input, result);
104 // MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
105 [Test]
106 public void RFC1321_Test4 ()
108 string className = hash.ToString ();
109 byte[] result = { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 };
110 byte[] input = Encoding.Default.GetBytes ("message digest");
112 string testName = className + " 4";
113 RFC1321_a (testName, hash, input, result);
114 RFC1321_b (testName, hash, input, result);
115 RFC1321_c (testName, hash, input, result);
116 RFC1321_d (testName, hash, input, result);
117 RFC1321_e (testName, hash, input, result);
120 // MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
121 [Test]
122 public void RFC1321_Test5 ()
124 string className = hash.ToString ();
125 byte[] result = { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b };
126 byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
128 string testName = className + " 5";
129 RFC1321_a (testName, hash, input, result);
130 RFC1321_b (testName, hash, input, result);
131 RFC1321_c (testName, hash, input, result);
132 RFC1321_d (testName, hash, input, result);
133 RFC1321_e (testName, hash, input, result);
136 // MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
137 // d174ab98d277d9f5a5611c2c9f419d9f
138 [Test]
139 public void RFC1321_Test6 ()
141 string className = hash.ToString ();
142 byte[] result = { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f };
143 byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
145 string testName = className + " 6";
146 RFC1321_a (testName, hash, input, result);
147 RFC1321_b (testName, hash, input, result);
148 RFC1321_c (testName, hash, input, result);
149 RFC1321_d (testName, hash, input, result);
150 RFC1321_e (testName, hash, input, result);
153 // MD5 ("123456789012345678901234567890123456789012345678901234567890123456
154 // 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
155 [Test]
156 public void RFC1321_Test7 ()
158 string className = hash.ToString ();
159 byte[] result = { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a };
160 byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
162 string testName = className + " 7";
163 RFC1321_a (testName, hash, input, result);
164 RFC1321_b (testName, hash, input, result);
165 RFC1321_c (testName, hash, input, result);
166 RFC1321_d (testName, hash, input, result);
167 RFC1321_e (testName, hash, input, result);
170 public void RFC1321_a (string testName, MD5 hash, byte[] input, byte[] result)
172 byte[] output = hash.ComputeHash (input);
173 AssertEquals (testName + ".a.1", result, output);
174 AssertEquals (testName + ".a.2", result, hash.Hash);
175 // required or next operation will still return old hash
176 hash.Initialize ();
179 public void RFC1321_b (string testName, MD5 hash, byte[] input, byte[] result)
181 byte[] output = hash.ComputeHash (input, 0, input.Length);
182 AssertEquals (testName + ".b.1", result, output);
183 AssertEquals (testName + ".b.2", result, hash.Hash);
184 // required or next operation will still return old hash
185 hash.Initialize ();
188 public void RFC1321_c (string testName, MD5 hash, byte[] input, byte[] result)
190 MemoryStream ms = new MemoryStream (input);
191 byte[] output = hash.ComputeHash (ms);
192 AssertEquals (testName + ".c.1", result, output);
193 AssertEquals (testName + ".c.2", result, hash.Hash);
194 // required or next operation will still return old hash
195 hash.Initialize ();
198 public void RFC1321_d (string testName, MD5 hash, byte[] input, byte[] result)
200 byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
201 AssertEquals (testName + ".d.1", input, output);
202 AssertEquals (testName + ".d.2", result, hash.Hash);
203 // required or next operation will still return old hash
204 hash.Initialize ();
207 public void RFC1321_e (string testName, MD5 hash, byte[] input, byte[] result)
209 byte[] copy = new byte [input.Length];
210 for (int i=0; i < input.Length - 1; i++)
211 hash.TransformBlock (input, i, 1, copy, i);
212 byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
213 Assertion.AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
214 AssertEquals (testName + ".e.2", result, hash.Hash);
215 // required or next operation will still return old hash
216 hash.Initialize ();
219 // none of those values changes for any implementation of MD5
220 public virtual void StaticInfo ()
222 string className = hash.ToString ();
223 Assertion.AssertEquals (className + ".HashSize", 128, hash.HashSize);
224 Assertion.AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
225 Assertion.AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);