2 // MD4Test.cs - NUnit Test Cases for MD4 (RFC1320)
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
13 using System.Security.Cryptography;
16 using Mono.Security.Cryptography;
17 using NUnit.Framework;
19 namespace MonoTests.Mono.Security.Cryptography {
22 // a. The MD4 Message-Digest Algorithm
23 // http://www.ietf.org/rfc/RFC1320.txt
25 // MD4 is a abstract class - so ALL of the test included here wont be tested
26 // on the abstract class but should be tested in ALL its descendants.
27 public abstract class MD4Test {
31 static void AssertEquals (string msg, int expected, int actual)
33 Assert.AreEqual (expected, actual, msg);
36 // because most crypto stuff works with byte[] buffers
37 static public void AssertEquals (string msg, byte[] array1, byte[] array2)
39 if ((array1 == null) && (array2 == null))
42 Assert.Fail (msg + " -> First array is NULL");
44 Assert.Fail (msg + " -> Second array is NULL");
46 bool a = (array1.Length == array2.Length);
48 for (int i = 0; i < array1.Length; i++) {
49 if (array1 [i] != array2 [i]) {
55 if (array1.Length > 0) {
56 msg += " -> Expected " + BitConverter.ToString (array1, 0);
57 msg += " is different than " + BitConverter.ToString (array2, 0);
59 Assert.IsTrue (a, msg);
62 // MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
64 public void RFC1320_Test1 ()
66 string className = hash.ToString ();
67 byte[] result = { 0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0 };
68 byte[] input = new byte [0];
70 string testName = className + " 1";
71 RFC1320_a (testName, hash, input, result);
72 RFC1320_b (testName, hash, input, result);
73 RFC1320_c (testName, hash, input, result);
74 RFC1320_d (testName, hash, input, result);
75 // N/A RFC1320_e (testName, hash, input, result);
78 // MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
80 public void RFC1320_Test2 ()
82 string className = hash.ToString ();
83 byte[] result = { 0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24 };
84 byte[] input = Encoding.Default.GetBytes ("a");
86 string testName = className + " 2";
87 RFC1320_a (testName, hash, input, result);
88 RFC1320_b (testName, hash, input, result);
89 RFC1320_c (testName, hash, input, result);
90 RFC1320_d (testName, hash, input, result);
91 RFC1320_e (testName, hash, input, result);
94 // MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
96 public void RFC1320_Test3 ()
98 string className = hash.ToString ();
99 byte[] result = { 0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d };
100 byte[] input = Encoding.Default.GetBytes ("abc");
102 string testName = className + " 3";
103 RFC1320_a (testName, hash, input, result);
104 RFC1320_b (testName, hash, input, result);
105 RFC1320_c (testName, hash, input, result);
106 RFC1320_d (testName, hash, input, result);
107 RFC1320_e (testName, hash, input, result);
110 // MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
112 public void RFC1320_Test4 ()
114 string className = hash.ToString ();
115 byte[] result = { 0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b };
116 byte[] input = Encoding.Default.GetBytes ("message digest");
118 string testName = className + " 4";
119 RFC1320_a (testName, hash, input, result);
120 RFC1320_b (testName, hash, input, result);
121 RFC1320_c (testName, hash, input, result);
122 RFC1320_d (testName, hash, input, result);
123 RFC1320_e (testName, hash, input, result);
126 // MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
128 public void RFC1320_Test5 ()
130 string className = hash.ToString ();
131 byte[] result = { 0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9 };
132 byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
134 string testName = className + " 5";
135 RFC1320_a (testName, hash, input, result);
136 RFC1320_b (testName, hash, input, result);
137 RFC1320_c (testName, hash, input, result);
138 RFC1320_d (testName, hash, input, result);
139 RFC1320_e (testName, hash, input, result);
142 // MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
143 // 043f8582f241db351ce627e153e7f0e4
145 public void RFC1320_Test6 ()
147 string className = hash.ToString ();
148 byte[] result = { 0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4 };
149 byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
151 string testName = className + " 6";
152 RFC1320_a (testName, hash, input, result);
153 RFC1320_b (testName, hash, input, result);
154 RFC1320_c (testName, hash, input, result);
155 RFC1320_d (testName, hash, input, result);
156 RFC1320_e (testName, hash, input, result);
159 // MD4 ("123456789012345678901234567890123456789012345678901234567890123456
160 // 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
162 public void RFC1320_Test7 ()
164 string className = hash.ToString ();
165 byte[] result = { 0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36 };
166 byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
168 string testName = className + " 7";
169 RFC1320_a (testName, hash, input, result);
170 RFC1320_b (testName, hash, input, result);
171 RFC1320_c (testName, hash, input, result);
172 RFC1320_d (testName, hash, input, result);
173 RFC1320_e (testName, hash, input, result);
176 public void RFC1320_a (string testName, MD4 hash, byte[] input, byte[] result)
178 byte[] output = hash.ComputeHash (input);
179 AssertEquals (testName + ".a.1", result, output);
180 AssertEquals (testName + ".a.2", result, hash.Hash);
181 // required or next operation will still return old hash
185 public void RFC1320_b (string testName, MD4 hash, byte[] input, byte[] result)
187 byte[] output = hash.ComputeHash (input, 0, input.Length);
188 AssertEquals (testName + ".b.1", result, output);
189 AssertEquals (testName + ".b.2", result, hash.Hash);
190 // required or next operation will still return old hash
194 public void RFC1320_c (string testName, MD4 hash, byte[] input, byte[] result)
196 MemoryStream ms = new MemoryStream (input);
197 byte[] output = hash.ComputeHash (ms);
198 AssertEquals (testName + ".c.1", result, output);
199 AssertEquals (testName + ".c.2", result, hash.Hash);
200 // required or next operation will still return old hash
204 public void RFC1320_d (string testName, MD4 hash, byte[] input, byte[] result)
206 byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
207 AssertEquals (testName + ".d.1", input, output);
208 AssertEquals (testName + ".d.2", result, hash.Hash);
209 // required or next operation will still return old hash
213 public void RFC1320_e (string testName, MD4 hash, byte[] input, byte[] result)
215 byte[] copy = new byte [input.Length];
216 for (int i=0; i < input.Length - 1; i++)
217 hash.TransformBlock (input, i, 1, copy, i);
218 byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
219 AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
220 AssertEquals (testName + ".e.2", result, hash.Hash);
221 // required or next operation will still return old hash
225 // none of those values changes for any implementation of MD4
227 public virtual void StaticInfo ()
229 string className = hash.ToString ();
230 AssertEquals (className + ".HashSize", 128, hash.HashSize);
231 AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
232 AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
236 public virtual void Create ()
238 // create the default implementation
239 HashAlgorithm h = MD4.Create ();
240 Assert.IsTrue ((h is MD4Managed), "MD4Managed");
241 // Note: will fail is default is changed in machine.config