2 // SHA224Test.cs - NUnit Test Cases for SHA224
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit
.Framework
;
32 using System
.Security
.Cryptography
;
34 using Mono
.Security
.Cryptography
;
36 namespace MonoTests
.System
.Security
.Cryptography
{
39 // a. RFC 3874 - A 224-bit One-way Hash Function: SHA-224, September 2004
40 // http://www.faqs.org/rfc/rfc3874.txt
41 // b. FIPS PUB 180-2: Secure Hash Standard
42 // http://csrc.nist.gov/publications/fips/fips180-2/fip180-2.txt
44 // SHA224 is a abstract class - so most of the test included here wont be tested
45 // on the abstract class but should be tested in ALL its descendants.
48 public class SHA224Test
{
50 protected HashAlgorithm hash
;
53 public virtual void SetUp ()
55 hash
= SHA224
.Create ();
58 internal void AssertEquals (string msg
, byte[] expected
, byte[] actual
)
60 if (expected
== null) {
62 string a
= BitConverter
.ToString (actual
);
63 Assert
.Fail (String
.Format ("{0} - Expected null value but got: {1}", msg
, a
));
66 else if (actual
== null) {
67 string e
= BitConverter
.ToString (expected
);
68 Assert
.Fail (String
.Format ("{0} - Got null value but expected: {1}", msg
, e
));
70 Assert
.AreEqual (BitConverter
.ToString (expected
), BitConverter
.ToString (actual
), msg
);
73 // test vectors from RFC 3874 (as NIST FIPS 186-2 hasn't yet
74 // been officially updated for SHA224 test vectors).
76 private string input1
= "abc";
77 private string input2
= "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
79 public void FIPS186_Test1 (SHA224 hash
)
81 string className
= hash
.ToString ();
82 byte[] result
= { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, 0x86, 0x42,
83 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, 0x2A, 0xAD, 0xBC, 0xE4,
84 0xBD, 0xA0, 0xB3, 0xF7, 0xE3, 0x6C, 0x9D, 0xA7 };
85 byte[] input
= Encoding
.Default
.GetBytes (input1
);
87 string testName
= className
+ " 1";
88 FIPS186_a (testName
, hash
, input
, result
);
89 FIPS186_b (testName
, hash
, input
, result
);
90 FIPS186_c (testName
, hash
, input
, result
);
91 FIPS186_d (testName
, hash
, input
, result
);
92 FIPS186_e (testName
, hash
, input
, result
);
95 public void FIPS186_Test2 (SHA224 hash
)
97 string className
= hash
.ToString ();
98 byte[] result
= { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, 0x5D, 0xBA,
99 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, 0xB0, 0xC6, 0x45, 0x5C,
100 0xB4, 0xF5, 0x8B, 0x19, 0x52, 0x52, 0x25, 0x25 };
101 byte[] input
= Encoding
.Default
.GetBytes (input2
);
103 string testName
= className
+ " 2";
104 FIPS186_a (testName
, hash
, input
, result
);
105 FIPS186_b (testName
, hash
, input
, result
);
106 FIPS186_c (testName
, hash
, input
, result
);
107 FIPS186_d (testName
, hash
, input
, result
);
108 FIPS186_e (testName
, hash
, input
, result
);
111 public void FIPS186_Test3 (SHA224 hash
)
113 string className
= hash
.ToString ();
114 byte [] result
= { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, 0xBB, 0xB4,
115 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, 0xF0, 0x3F, 0x42, 0x58,
116 0x19, 0x48, 0xB2, 0xEE, 0x4E, 0xE7, 0xAD, 0x67 };
117 byte[] input
= new byte [1000000];
118 for (int i
= 0; i
< 1000000; i
++)
119 input
[i
] = 0x61; // a
121 string testName
= className
+ " 3";
122 FIPS186_a (testName
, hash
, input
, result
);
123 FIPS186_b (testName
, hash
, input
, result
);
124 FIPS186_c (testName
, hash
, input
, result
);
125 FIPS186_d (testName
, hash
, input
, result
);
126 FIPS186_e (testName
, hash
, input
, result
);
129 public void FIPS186_a (string testName
, SHA224 hash
, byte[] input
, byte[] result
)
131 byte[] output
= hash
.ComputeHash (input
);
132 AssertEquals (testName
+ ".a.1", result
, output
);
133 AssertEquals (testName
+ ".a.2", result
, hash
.Hash
);
134 // required or next operation will still return old hash
138 public void FIPS186_b (string testName
, SHA224 hash
, byte[] input
, byte[] result
)
140 byte[] output
= hash
.ComputeHash (input
, 0, input
.Length
);
141 AssertEquals (testName
+ ".b.1", result
, output
);
142 AssertEquals (testName
+ ".b.2", result
, hash
.Hash
);
143 // required or next operation will still return old hash
147 public void FIPS186_c (string testName
, SHA224 hash
, byte[] input
, byte[] result
)
149 MemoryStream ms
= new MemoryStream (input
);
150 byte[] output
= hash
.ComputeHash (ms
);
151 AssertEquals (testName
+ ".c.1", result
, output
);
152 AssertEquals (testName
+ ".c.2", result
, hash
.Hash
);
153 // required or next operation will still return old hash
157 public void FIPS186_d (string testName
, SHA224 hash
, byte[] input
, byte[] result
)
159 hash
.TransformFinalBlock (input
, 0, input
.Length
);
160 // Note: TransformFinalBlock doesn't return HashValue !
161 // AssertEquals( testName + ".d.1", result, output );
162 AssertEquals (testName
+ ".d", result
, hash
.Hash
);
163 // required or next operation will still return old hash
167 public void FIPS186_e (string testName
, SHA224 hash
, byte[] input
, byte[] result
)
169 byte[] copy
= new byte [input
.Length
];
170 for (int i
=0; i
< input
.Length
- 1; i
++)
171 hash
.TransformBlock (input
, i
, 1, copy
, i
);
172 hash
.TransformFinalBlock (input
, input
.Length
- 1, 1);
173 // Note: TransformFinalBlock doesn't return HashValue !
174 // AssertEquals (testName + ".e.1", result, output);
175 AssertEquals (testName
+ ".e", result
, hash
.Hash
);
176 // required or next operation will still return old hash
181 public virtual void Create ()
183 // Note: These tests will only be valid without a "machine.config" file
184 // or a "machine.config" file that do not modify the default algorithm
186 // FIXME: Change namespace when (or if) classes are moved into corlib
187 const string defaultSHA224
= "Mono.Security.Cryptography.SHA224Managed";
189 // try to build the default implementation
190 SHA224 hash
= SHA224
.Create ();
191 Assert
.AreEqual (hash
.ToString (), defaultSHA224
, "SHA224.Create()");
193 // try to build, in every way, a SHA224 implementation
194 hash
= SHA224
.Create ("SHA224");
195 Assert
.AreEqual (hash
.ToString (), defaultSHA224
, "SHA224.Create('SHA224')");
196 hash
= SHA224
.Create ("SHA-224");
197 Assert
.AreEqual (hash
.ToString (), defaultSHA224
, "SHA224.Create('SHA-224')");
201 [ExpectedException (typeof (InvalidCastException
))]
202 public void CreateIncorrect ()
204 // try to build an incorrect hash algorithms
205 hash
= SHA224
.Create ("MD5");
208 /* Uncomment when (or if) the SHA224 classes are moved into corlib
210 public void CreateInvalid ()
212 // try to build invalid implementation
213 hash = SHA224.Create ("InvalidHash");
214 Assert.IsNull (hash, "SHA224.Create('InvalidHash')");
218 [ExpectedException (typeof (ArgumentNullException
))]
219 public void CreateNull ()
221 // try to build null implementation
222 hash
= SHA224
.Create (null);
225 // none of those values changes for any implementation of defaultSHA224
227 public virtual void StaticInfo ()
229 string className
= hash
.ToString ();
230 Assert
.AreEqual (224, hash
.HashSize
, className
+ ".HashSize");
231 Assert
.AreEqual (1, hash
.InputBlockSize
, className
+ ".InputBlockSize");
232 Assert
.AreEqual (1, hash
.OutputBlockSize
, className
+ ".OutputBlockSize");