2 // MD2Test.cs - NUnit Test Cases for MD2 (RFC1319)
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2002 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 MD2 Message-Digest Algorithm
23 // http://www.ietf.org/rfc/rfc1319.txt
25 // MD2 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 MD2Test
: 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 Fail (msg
+ " -> First array is NULL");
39 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);
57 // MD2 ("") = 8350e5a3e24c153df2275c9f80692773
59 public void RFC1319_Test1 ()
61 string className
= hash
.ToString ();
62 byte[] result
= { 0x83, 0x50, 0xe5, 0xa3, 0xe2, 0x4c, 0x15, 0x3d, 0xf2, 0x27, 0x5c, 0x9f, 0x80, 0x69, 0x27, 0x73 }
;
63 byte[] input
= new byte [0];
65 string testName
= className
+ " 1";
66 RFC1319_a (testName
, hash
, input
, result
);
67 RFC1319_b (testName
, hash
, input
, result
);
68 RFC1319_c (testName
, hash
, input
, result
);
69 RFC1319_d (testName
, hash
, input
, result
);
70 // N/A RFC1319_e (testName, hash, input, result);
73 // MD2 ("a") = 32ec01ec4a6dac72c0ab96fb34c0b5d1
75 public void RFC1319_Test2 ()
77 string className
= hash
.ToString ();
78 byte[] result
= { 0x32, 0xec, 0x01, 0xec, 0x4a, 0x6d, 0xac, 0x72, 0xc0, 0xab, 0x96, 0xfb, 0x34, 0xc0, 0xb5, 0xd1 }
;
79 byte[] input
= Encoding
.Default
.GetBytes ("a");
81 string testName
= className
+ " 2";
82 RFC1319_a (testName
, hash
, input
, result
);
83 RFC1319_b (testName
, hash
, input
, result
);
84 RFC1319_c (testName
, hash
, input
, result
);
85 RFC1319_d (testName
, hash
, input
, result
);
86 RFC1319_e (testName
, hash
, input
, result
);
89 // MD2 ("abc") = da853b0d3f88d99b30283a69e6ded6bb
91 public void RFC1319_Test3 ()
93 string className
= hash
.ToString ();
94 byte[] result
= { 0xda, 0x85, 0x3b, 0x0d, 0x3f, 0x88, 0xd9, 0x9b, 0x30, 0x28, 0x3a, 0x69, 0xe6, 0xde, 0xd6, 0xbb }
;
95 byte[] input
= Encoding
.Default
.GetBytes ("abc");
97 string testName
= className
+ " 3";
98 RFC1319_a (testName
, hash
, input
, result
);
99 RFC1319_b (testName
, hash
, input
, result
);
100 RFC1319_c (testName
, hash
, input
, result
);
101 RFC1319_d (testName
, hash
, input
, result
);
102 RFC1319_e (testName
, hash
, input
, result
);
105 // MD2 ("message digest") = ab4f496bfb2a530b219ff33031fe06b0
107 public void RFC1319_Test4 ()
109 string className
= hash
.ToString ();
110 byte[] result
= { 0xab, 0x4f, 0x49, 0x6b, 0xfb, 0x2a, 0x53, 0x0b, 0x21, 0x9f, 0xf3, 0x30, 0x31, 0xfe, 0x06, 0xb0 }
;
111 byte[] input
= Encoding
.Default
.GetBytes ("message digest");
113 string testName
= className
+ " 4";
114 RFC1319_a (testName
, hash
, input
, result
);
115 RFC1319_b (testName
, hash
, input
, result
);
116 RFC1319_c (testName
, hash
, input
, result
);
117 RFC1319_d (testName
, hash
, input
, result
);
118 RFC1319_e (testName
, hash
, input
, result
);
121 // MD2 ("abcdefghijklmnopqrstuvwxyz") = 4e8ddff3650292ab5a4108c3aa47940b
123 public void RFC1319_Test5 ()
125 string className
= hash
.ToString ();
126 byte[] result
= { 0x4e, 0x8d, 0xdf, 0xf3, 0x65, 0x02, 0x92, 0xab, 0x5a, 0x41, 0x08, 0xc3, 0xaa, 0x47, 0x94, 0x0b }
;
127 byte[] input
= Encoding
.Default
.GetBytes ("abcdefghijklmnopqrstuvwxyz");
129 string testName
= className
+ " 5";
130 RFC1319_a (testName
, hash
, input
, result
);
131 RFC1319_b (testName
, hash
, input
, result
);
132 RFC1319_c (testName
, hash
, input
, result
);
133 RFC1319_d (testName
, hash
, input
, result
);
134 RFC1319_e (testName
, hash
, input
, result
);
137 // MD2 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
138 // da33def2a42df13975352846c30338cd
140 public void RFC1319_Test6 ()
142 string className
= hash
.ToString ();
143 byte[] result
= { 0xda, 0x33, 0xde, 0xf2, 0xa4, 0x2d, 0xf1, 0x39, 0x75, 0x35, 0x28, 0x46, 0xc3, 0x03, 0x38, 0xcd }
;
144 byte[] input
= Encoding
.Default
.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
146 string testName
= className
+ " 6";
147 RFC1319_a (testName
, hash
, input
, result
);
148 RFC1319_b (testName
, hash
, input
, result
);
149 RFC1319_c (testName
, hash
, input
, result
);
150 RFC1319_d (testName
, hash
, input
, result
);
151 RFC1319_e (testName
, hash
, input
, result
);
154 // MD2 ("123456789012345678901234567890123456789012345678901234567890123456
155 // 78901234567890") = d5976f79d83d3a0dc9806c3c66f3efd8
157 public void RFC1319_Test7 ()
159 string className
= hash
.ToString ();
160 byte[] result
= { 0xd5, 0x97, 0x6f, 0x79, 0xd8, 0x3d, 0x3a, 0x0d, 0xc9, 0x80, 0x6c, 0x3c, 0x66, 0xf3, 0xef, 0xd8 }
;
161 byte[] input
= Encoding
.Default
.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
163 string testName
= className
+ " 7";
164 RFC1319_a (testName
, hash
, input
, result
);
165 RFC1319_b (testName
, hash
, input
, result
);
166 RFC1319_c (testName
, hash
, input
, result
);
167 RFC1319_d (testName
, hash
, input
, result
);
168 RFC1319_e (testName
, hash
, input
, result
);
171 public void RFC1319_a (string testName
, MD2 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 RFC1319_b (string testName
, MD2 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 RFC1319_c (string testName
, MD2 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 RFC1319_d (string testName
, MD2 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 RFC1319_e (string testName
, MD2 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 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 MD2
222 public virtual void StaticInfo ()
224 string className
= hash
.ToString ();
225 AssertEquals (className
+ ".HashSize", 128, hash
.HashSize
);
226 AssertEquals (className
+ ".InputBlockSize", 1, hash
.InputBlockSize
);
227 AssertEquals (className
+ ".OutputBlockSize", 1, hash
.OutputBlockSize
);
231 public virtual void Create ()
233 // create the default implementation
234 HashAlgorithm h
= MD2
.Create ();
235 Assert ("MD2Managed", (h
is MD2Managed
));
236 // Note: will fail is default is changed in machine.config