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 class MD4Test
: Assertion
{
31 // because most crypto stuff works with byte[] buffers
32 static public void AssertEquals (string msg
, byte[] array1
, byte[] array2
)
34 if ((array1
== null) && (array2
== null))
37 Assertion
.Fail (msg
+ " -> First array is NULL");
39 Assertion
.Fail (msg
+ " -> Second array is NULL");
41 bool a
= (array1
.Length
== array2
.Length
);
43 for (int i
= 0; i
< array1
.Length
; i
++) {
44 if (array1
[i
] != array2
[i
]) {
50 if (array1
.Length
> 0) {
51 msg
+= " -> Expected " + BitConverter
.ToString (array1
, 0);
52 msg
+= " is different than " + BitConverter
.ToString (array2
, 0);
54 Assertion
.Assert (msg
, a
);
57 // MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
59 public void RFC1320_Test1 ()
61 string className
= hash
.ToString ();
62 byte[] result
= { 0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0 }
;
63 byte[] input
= new byte [0];
65 string testName
= className
+ " 1";
66 RFC1320_a (testName
, hash
, input
, result
);
67 RFC1320_b (testName
, hash
, input
, result
);
68 RFC1320_c (testName
, hash
, input
, result
);
69 RFC1320_d (testName
, hash
, input
, result
);
70 // N/A RFC1320_e (testName, hash, input, result);
73 // MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
75 public void RFC1320_Test2 ()
77 string className
= hash
.ToString ();
78 byte[] result
= { 0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24 }
;
79 byte[] input
= Encoding
.Default
.GetBytes ("a");
81 string testName
= className
+ " 2";
82 RFC1320_a (testName
, hash
, input
, result
);
83 RFC1320_b (testName
, hash
, input
, result
);
84 RFC1320_c (testName
, hash
, input
, result
);
85 RFC1320_d (testName
, hash
, input
, result
);
86 RFC1320_e (testName
, hash
, input
, result
);
89 // MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
91 public void RFC1320_Test3 ()
93 string className
= hash
.ToString ();
94 byte[] result
= { 0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d }
;
95 byte[] input
= Encoding
.Default
.GetBytes ("abc");
97 string testName
= className
+ " 3";
98 RFC1320_a (testName
, hash
, input
, result
);
99 RFC1320_b (testName
, hash
, input
, result
);
100 RFC1320_c (testName
, hash
, input
, result
);
101 RFC1320_d (testName
, hash
, input
, result
);
102 RFC1320_e (testName
, hash
, input
, result
);
105 // MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
107 public void RFC1320_Test4 ()
109 string className
= hash
.ToString ();
110 byte[] result
= { 0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b }
;
111 byte[] input
= Encoding
.Default
.GetBytes ("message digest");
113 string testName
= className
+ " 4";
114 RFC1320_a (testName
, hash
, input
, result
);
115 RFC1320_b (testName
, hash
, input
, result
);
116 RFC1320_c (testName
, hash
, input
, result
);
117 RFC1320_d (testName
, hash
, input
, result
);
118 RFC1320_e (testName
, hash
, input
, result
);
121 // MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
123 public void RFC1320_Test5 ()
125 string className
= hash
.ToString ();
126 byte[] result
= { 0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9 }
;
127 byte[] input
= Encoding
.Default
.GetBytes ("abcdefghijklmnopqrstuvwxyz");
129 string testName
= className
+ " 5";
130 RFC1320_a (testName
, hash
, input
, result
);
131 RFC1320_b (testName
, hash
, input
, result
);
132 RFC1320_c (testName
, hash
, input
, result
);
133 RFC1320_d (testName
, hash
, input
, result
);
134 RFC1320_e (testName
, hash
, input
, result
);
137 // MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
138 // 043f8582f241db351ce627e153e7f0e4
140 public void RFC1320_Test6 ()
142 string className
= hash
.ToString ();
143 byte[] result
= { 0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4 }
;
144 byte[] input
= Encoding
.Default
.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
146 string testName
= className
+ " 6";
147 RFC1320_a (testName
, hash
, input
, result
);
148 RFC1320_b (testName
, hash
, input
, result
);
149 RFC1320_c (testName
, hash
, input
, result
);
150 RFC1320_d (testName
, hash
, input
, result
);
151 RFC1320_e (testName
, hash
, input
, result
);
154 // MD4 ("123456789012345678901234567890123456789012345678901234567890123456
155 // 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
157 public void RFC1320_Test7 ()
159 string className
= hash
.ToString ();
160 byte[] result
= { 0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36 }
;
161 byte[] input
= Encoding
.Default
.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
163 string testName
= className
+ " 7";
164 RFC1320_a (testName
, hash
, input
, result
);
165 RFC1320_b (testName
, hash
, input
, result
);
166 RFC1320_c (testName
, hash
, input
, result
);
167 RFC1320_d (testName
, hash
, input
, result
);
168 RFC1320_e (testName
, hash
, input
, result
);
171 public void RFC1320_a (string testName
, MD4 hash
, byte[] input
, byte[] result
)
173 byte[] output
= hash
.ComputeHash (input
);
174 AssertEquals (testName
+ ".a.1", result
, output
);
175 AssertEquals (testName
+ ".a.2", result
, hash
.Hash
);
176 // required or next operation will still return old hash
180 public void RFC1320_b (string testName
, MD4 hash
, byte[] input
, byte[] result
)
182 byte[] output
= hash
.ComputeHash (input
, 0, input
.Length
);
183 AssertEquals (testName
+ ".b.1", result
, output
);
184 AssertEquals (testName
+ ".b.2", result
, hash
.Hash
);
185 // required or next operation will still return old hash
189 public void RFC1320_c (string testName
, MD4 hash
, byte[] input
, byte[] result
)
191 MemoryStream ms
= new MemoryStream (input
);
192 byte[] output
= hash
.ComputeHash (ms
);
193 AssertEquals (testName
+ ".c.1", result
, output
);
194 AssertEquals (testName
+ ".c.2", result
, hash
.Hash
);
195 // required or next operation will still return old hash
199 public void RFC1320_d (string testName
, MD4 hash
, byte[] input
, byte[] result
)
201 byte[] output
= hash
.TransformFinalBlock (input
, 0, input
.Length
);
202 AssertEquals (testName
+ ".d.1", input
, output
);
203 AssertEquals (testName
+ ".d.2", result
, hash
.Hash
);
204 // required or next operation will still return old hash
208 public void RFC1320_e (string testName
, MD4 hash
, byte[] input
, byte[] result
)
210 byte[] copy
= new byte [input
.Length
];
211 for (int i
=0; i
< input
.Length
- 1; i
++)
212 hash
.TransformBlock (input
, i
, 1, copy
, i
);
213 byte[] output
= hash
.TransformFinalBlock (input
, input
.Length
- 1, 1);
214 TestCase
.AssertEquals (testName
+ ".e.1", input
[input
.Length
- 1], output
[0]);
215 AssertEquals (testName
+ ".e.2", result
, hash
.Hash
);
216 // required or next operation will still return old hash
220 // none of those values changes for any implementation of MD4
222 public virtual void StaticInfo ()
224 string className
= hash
.ToString ();
225 TestCase
.AssertEquals (className
+ ".HashSize", 128, hash
.HashSize
);
226 TestCase
.AssertEquals (className
+ ".InputBlockSize", 1, hash
.InputBlockSize
);
227 TestCase
.AssertEquals (className
+ ".OutputBlockSize", 1, hash
.OutputBlockSize
);
231 public virtual void Create ()
233 // create the default implementation
234 HashAlgorithm h
= MD4
.Create ();
235 Assert ("MD4Managed", (h
is MD4Managed
));
236 // Note: will fail is default is changed in machine.config