2 // SHA512Test.cs - NUnit Test Cases for SHA512
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell http://www.novell.com
11 using NUnit.Framework;
14 using System.Security.Cryptography;
17 namespace MonoTests.System.Security.Cryptography {
20 // a. FIPS PUB 180-2: Secure Hash Standard
21 // http://csrc.nist.gov/publications/fips/fips180-2/fip180-2.txt
23 // SHA512 is a abstract class - so most of the test included here wont be tested
24 // on the abstract class but should be tested in ALL its descendants.
27 public class SHA512Test : HashAlgorithmTest {
30 protected override void SetUp ()
32 hash = SHA512.Create ();
35 // test vectors from NIST FIPS 186-2
37 private string input1 = "abc";
38 private string input2 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
40 public void FIPS186_Test1 (SHA512 hash)
42 string className = hash.ToString ();
43 byte[] result = { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
44 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
45 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
46 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
47 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
48 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
49 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
50 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f };
51 byte[] input = Encoding.Default.GetBytes (input1);
53 string testName = className + " 1";
54 FIPS186_a (testName, hash, input, result);
55 FIPS186_b (testName, hash, input, result);
56 FIPS186_c (testName, hash, input, result);
57 FIPS186_d (testName, hash, input, result);
58 FIPS186_e (testName, hash, input, result);
61 public void FIPS186_Test2 (SHA512 hash)
63 string className = hash.ToString ();
64 byte[] result = { 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
65 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
66 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
67 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
68 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
69 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
70 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
71 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 };
72 byte[] input = Encoding.Default.GetBytes (input2);
74 string testName = className + " 2";
75 FIPS186_a (testName, hash, input, result);
76 FIPS186_b (testName, hash, input, result);
77 FIPS186_c (testName, hash, input, result);
78 FIPS186_d (testName, hash, input, result);
79 FIPS186_e (testName, hash, input, result);
82 public void FIPS186_Test3 (SHA512 hash)
84 string className = hash.ToString ();
85 byte[] result = { 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64,
86 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
87 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28,
88 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
89 0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a,
90 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b,
91 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e,
92 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b };
93 byte[] input = new byte [1000000];
94 for (int i = 0; i < 1000000; i++)
97 string testName = className + " 3";
98 FIPS186_a (testName, hash, input, result);
99 FIPS186_b (testName, hash, input, result);
100 FIPS186_c (testName, hash, input, result);
101 FIPS186_d (testName, hash, input, result);
102 FIPS186_e (testName, hash, input, result);
105 public void FIPS186_a (string testName, SHA512 hash, byte[] input, byte[] result)
107 byte[] output = hash.ComputeHash (input);
108 AssertEquals (testName + ".a.1", result, output);
109 AssertEquals (testName + ".a.2", result, hash.Hash);
110 // required or next operation will still return old hash
114 public void FIPS186_b (string testName, SHA512 hash, byte[] input, byte[] result)
116 byte[] output = hash.ComputeHash (input, 0, input.Length);
117 AssertEquals (testName + ".b.1", result, output);
118 AssertEquals (testName + ".b.2", result, hash.Hash);
119 // required or next operation will still return old hash
123 public void FIPS186_c (string testName, SHA512 hash, byte[] input, byte[] result)
125 MemoryStream ms = new MemoryStream (input);
126 byte[] output = hash.ComputeHash (ms);
127 AssertEquals (testName + ".c.1", result, output);
128 AssertEquals (testName + ".c.2", result, hash.Hash);
129 // required or next operation will still return old hash
133 public void FIPS186_d (string testName, SHA512 hash, byte[] input, byte[] result)
135 byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
136 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
137 // AssertEquals( testName + ".d.1", result, output );
138 AssertEquals (testName + ".d", result, hash.Hash);
139 // required or next operation will still return old hash
143 public void FIPS186_e (string testName, SHA512 hash, byte[] input, byte[] result)
145 byte[] copy = new byte [input.Length];
146 for (int i=0; i < input.Length - 1; i++)
147 hash.TransformBlock (input, i, 1, copy, i);
148 byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
149 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
150 // AssertEquals (testName + ".e.1", result, output);
151 AssertEquals (testName + ".e", result, hash.Hash);
152 // required or next operation will still return old hash
157 public override void Create ()
159 // Note: These tests will only be valid without a "machine.config" file
160 // or a "machine.config" file that do not modify the default algorithm
162 const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
164 // try to build the default implementation
165 SHA512 hash = SHA512.Create ();
166 AssertEquals ("SHA512.Create()", hash.ToString (), defaultSHA512);
168 // try to build, in every way, a SHA512 implementation
169 hash = SHA512.Create ("SHA512");
170 AssertEquals ("SHA512.Create('SHA512')", hash.ToString (), defaultSHA512);
171 hash = SHA512.Create ("SHA-512");
172 AssertEquals ("SHA512.Create('SHA-512')", hash.ToString (), defaultSHA512);
176 [ExpectedException (typeof (InvalidCastException))]
177 public void CreateIncorrect ()
179 // try to build an incorrect hash algorithms
180 hash = SHA512.Create ("MD5");
184 public void CreateInvalid ()
186 // try to build invalid implementation
187 hash = SHA512.Create ("InvalidHash");
188 AssertNull ("SHA512.Create('InvalidHash')", hash);
192 [ExpectedException (typeof (ArgumentNullException))]
193 public override void CreateNull ()
195 // try to build null implementation
196 hash = SHA512.Create (null);
199 // none of those values changes for any implementation of defaultSHA512
201 public virtual void StaticInfo ()
203 string className = hash.ToString ();
204 AssertEquals (className + ".HashSize", 512, hash.HashSize);
205 AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
206 AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);