Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/mcs
diff options
context:
space:
mode:
authorSebastien Pouliot <sebastien@ximian.com>2002-10-13 07:05:30 +0400
committerSebastien Pouliot <sebastien@ximian.com>2002-10-13 07:05:30 +0400
commit42e3890563c13305eb52208e211915711267d7c9 (patch)
tree0ad1dfd1b757184e7eb3cc5e7005c4ed92f1b1a5 /mcs
parent6bdc1a4369956603ef2b77b7c988e1dc1e81fcf6 (diff)
2002-10-12 Sebastien Pouliot <spouliot@videotron.ca>
* AllTests.cs: Added suite for RandomNumberGenerator, SHA1 and SHA1CryptoServiceProvider * RandomNumberGeneratorTest.cs: New. Tests for Create and generic random quality tests (FIPS140-2) * SHA1Test.cs: New. Tests for Create and generic SHA1 tests (FIPS180-1) * SHA1CryptoServiceProviderTest.cs: New. Inherited SHA1Tests tests + specific tests svn path=/trunk/mcs/; revision=8207
Diffstat (limited to 'mcs')
-rw-r--r--mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog10
-rw-r--r--mcs/class/corlib/Test/System.Security.Cryptography/HashAlgorithmTest.cs127
-rw-r--r--mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs180
-rw-r--r--mcs/class/corlib/Test/System.Security.Cryptography/SHA1CryptoServiceProviderTest.cs92
-rw-r--r--mcs/class/corlib/Test/System.Security.Cryptography/SHA1Test.cs210
5 files changed, 619 insertions, 0 deletions
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog b/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
index 9dbd9a67260..07e4608c067 100644
--- a/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
+++ b/mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
@@ -1,3 +1,13 @@
+2002-10-12 Sebastien Pouliot <spouliot@videotron.ca>
+
+ * AllTests.cs: Added suite for RandomNumberGenerator, SHA1 and
+ SHA1CryptoServiceProvider
+ * RandomNumberGeneratorTest.cs: New. Tests for Create and generic
+ random quality tests (FIPS140-2)
+ * SHA1Test.cs: New. Tests for Create and generic SHA1 tests (FIPS180-1)
+ * SHA1CryptoServiceProviderTest.cs: New. Inherited SHA1Tests tests +
+ specific tests
+
2002-10-10 Sebastien Pouliot <spouliot@videotron.ca>
* DSATest.cs: Added non-abstract DSA class to test To/FromXmlString()
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/HashAlgorithmTest.cs b/mcs/class/corlib/Test/System.Security.Cryptography/HashAlgorithmTest.cs
new file mode 100644
index 00000000000..9ec13aa1f86
--- /dev/null
+++ b/mcs/class/corlib/Test/System.Security.Cryptography/HashAlgorithmTest.cs
@@ -0,0 +1,127 @@
+//
+// HashAlgorithmTest.cs - NUnit Test Cases for HashAlgorithm
+//
+// Author:
+// Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using NUnit.Framework;
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MonoTests.System.Security.Cryptography
+{
+
+// HashAlgorithm is a abstract class - so most of it's functionality wont
+// be tested here (but will be in its descendants).
+public class HashAlgorithmTest : TestCase {
+ protected HashAlgorithm hash;
+
+ public HashAlgorithmTest () : base ("System.Security.Cryptography.HashAlgorithm testsuite") {}
+ public HashAlgorithmTest (string name) : base(name) {}
+
+ protected override void SetUp ()
+ {
+ hash = HashAlgorithm.Create ();
+ }
+
+ protected override void TearDown () {}
+
+ public static ITest Suite {
+ get {
+ return new TestSuite (typeof (HashAlgorithmTest));
+ }
+ }
+
+ // Note: These tests will only be valid without a "machine.config" file
+ // or a "machine.config" file that do not modify the default algorithm
+ // configuration.
+ private const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
+ private const string defaultMD5 = "System.Security.Cryptography.MD5CryptoServiceProvider";
+ private const string defaultSHA256 = "System.Security.Cryptography.SHA256Managed";
+ private const string defaultSHA384 = "System.Security.Cryptography.SHA384Managed";
+ private const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
+ private const string defaultHash = defaultSHA1;
+
+ public virtual void TestCreate ()
+ {
+ // try the default hash algorithm (created in SetUp)
+ AssertEquals( "HashAlgorithm.Create()", defaultHash, hash.ToString());
+
+ // try to build all hash algorithms
+ hash = HashAlgorithm.Create ("SHA");
+ AssertEquals ("HashAlgorithm.Create('SHA')", defaultSHA1, hash.ToString ());
+ hash = HashAlgorithm.Create ("SHA1");
+ AssertEquals ("HashAlgorithm.Create('SHA1')", defaultSHA1, hash.ToString ());
+ hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA1");
+ AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA1')", defaultSHA1, hash.ToString ());
+ hash = HashAlgorithm.Create ("System.Security.Cryptography.HashAlgorithm" );
+ AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.HashAlgorithm')", defaultHash, hash.ToString ());
+
+ hash = HashAlgorithm.Create ("MD5");
+ AssertEquals ("HashAlgorithm.Create('MD5')", defaultMD5, hash.ToString ());
+ hash = HashAlgorithm.Create ("System.Security.Cryptography.MD5");
+ AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.MD5')", defaultMD5, hash.ToString ());
+
+ hash = HashAlgorithm.Create ("SHA256");
+ AssertEquals ("HashAlgorithm.Create('SHA256')", defaultSHA256, hash.ToString ());
+ hash = HashAlgorithm.Create ("SHA-256");
+ AssertEquals ("HashAlgorithm.Create('SHA-256')", defaultSHA256, hash.ToString ());
+ hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA256");
+ AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA256')", defaultSHA256, hash.ToString ());
+
+ hash = HashAlgorithm.Create ("SHA384");
+ AssertEquals ("HashAlgorithm.Create('SHA384')", defaultSHA384, hash.ToString ());
+ hash = HashAlgorithm.Create ("SHA-384");
+ AssertEquals ("HashAlgorithm.Create('SHA-384')", defaultSHA384, hash.ToString ());
+ hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA384");
+ AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA384')", defaultSHA384, hash.ToString ());
+
+ hash = HashAlgorithm.Create ("SHA512");
+ AssertEquals ("HashAlgorithm.Create('SHA512')", defaultSHA512, hash.ToString ());
+ hash = HashAlgorithm.Create ("SHA-512");
+ AssertEquals ("HashAlgorithm.Create('SHA-512')", defaultSHA512, hash.ToString ());
+ hash = HashAlgorithm.Create ("System.Security.Cryptography.SHA512");
+ AssertEquals ("HashAlgorithm.Create('System.Security.Cryptography.SHA512')", defaultSHA512, hash.ToString ());
+
+ // try to build invalid implementation
+ hash = HashAlgorithm.Create ("InvalidHash");
+ AssertNull ("HashAlgorithm.Create('InvalidHash')", hash);
+
+ // try to build null implementation
+ try {
+ hash = HashAlgorithm.Create (null);
+ Fail ("HashAlgorithm.Create(null) should throw ArgumentNullException");
+ }
+ catch (ArgumentNullException) {
+ // do nothing, this is what we expect
+ }
+ catch (Exception e) {
+ Fail ("HashAlgorithm.Create(null) should throw ArgumentNullException not " + e.ToString() );
+ }
+ }
+
+ public void TestClear ()
+ {
+ byte[] inputABC = Encoding.Default.GetBytes ("abc");
+ hash.ComputeHash (inputABC);
+ hash.Clear ();
+ // cannot use a disposed object
+ try {
+ hash.ComputeHash (inputABC);
+ Fail ("ComputeHash after clear should throw ObjectDisposedException but didn't");
+ }
+ catch (ObjectDisposedException) {
+ // do nothing, this is what we expect
+ }
+ catch (Exception e) {
+ Fail ("ComputeHash after clear should throw ObjectDisposedException not " + e.ToString ());
+ }
+ }
+
+}
+
+}
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs b/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs
new file mode 100644
index 00000000000..d777b3d96d7
--- /dev/null
+++ b/mcs/class/corlib/Test/System.Security.Cryptography/RandomNumberGeneratorTest.cs
@@ -0,0 +1,180 @@
+//
+// RandomNumberGeneratorTest.cs - NUnit Test Cases for RNG
+//
+// Author:
+// Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using NUnit.Framework;
+using System;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MonoTests.System.Security.Cryptography {
+
+// References:
+// a. NIST FIPS PUB 140-2: Security requirements for Cryptographic Modules
+// http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf
+// b. NIST SP 800-22: A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications
+// not implemented
+// http://csrc.nist.gov/publications/nistpubs/800-22/sp-800-22-051501.pdf
+// c. IETF RFC1750: Randomness Recommendations for Security
+// not implemented
+// http://www.ietf.org/rfc/rfc1750.txt
+
+public class RandomNumberGeneratorTest : TestCase {
+ public RandomNumberGeneratorTest () : base ("System.Security.Cryptography.RandomNumberGenerator testsuite") {}
+ public RandomNumberGeneratorTest (string name) : base (name) {}
+
+ protected RandomNumberGenerator rng;
+
+ protected override void SetUp ()
+ {
+ rng = RandomNumberGenerator.Create();
+ }
+
+ protected override void TearDown () {}
+
+ public static ITest Suite {
+ get {
+ return new TestSuite (typeof (RandomNumberGeneratorTest));
+ }
+ }
+
+ public void AssertEquals (string msg, byte[] array1, byte[] array2)
+ {
+ AllTests.AssertEquals (msg, array1, array2);
+ }
+
+ // count the number of 1
+ protected void Monobit (string rngName, byte[] sample)
+ {
+ int x = 0;
+ for (int i=0; i < sample.Length; i++) {
+ byte b = sample[i];
+ for (int j = 0; j < 8; j++) {
+ if ((b & 0x01) == 0x01)
+ x++;
+ // next bit
+ b >>= 1;
+ }
+ }
+ Assert (rngName + " Monobit x=" + x, ((9725 < x) && (x < 10275)));
+ }
+
+ // 16 patterns (nibbles)
+ protected void Poker (string rngName, byte[] sample)
+ {
+ int[] pattern = new int[16];
+ for (int i = 0; i < sample.Length; i++) {
+ byte b = sample[i];
+ int n = (b & 0x0F);
+ pattern[n]++;
+ b >>= 4;
+ n = b;
+ pattern[n]++;
+ }
+ double result = 0;
+ for (int i = 0; i < 16; i++)
+ result += (pattern[i] * pattern[i]);
+ result = ((16 * result) / 5000) - 5000;
+ Assert (rngName + " Poker: " + result, ((result > 2.16) && (result < 46.17)));
+ }
+
+ // runs of 1 (or 0)
+ protected void Runs (string rngName, byte[] sample)
+ {
+ int[] runs = new int[6];
+ int x = 0;
+ bool one = false;
+ bool zero = false;
+ for (int i = sample.Length - 1; i >= 0 ; i--) {
+ byte b = sample[i];
+ for (int j = 0; j < 8; j++) {
+ if ((b & 0x01) == 0x01) {
+ if (!one) {
+ one = true;
+ zero = false;
+ int p = Math.Min (x, 6) - 1;
+ if (p >= 0)
+ runs[p]++;
+ x = 0;
+ }
+ }
+ else {
+ if (!zero) {
+ one = false;
+ zero = true;
+ /*int p = Math.Min (x, 6) - 1;
+ if (p >= 0)
+ runs[p]++;*/
+ x = 0;
+ }
+ }
+ x++;
+ // next bit
+ b >>= 1;
+ }
+ }
+ Assert (rngName + " Runs length=1: " + runs[0], ((runs[0] >= 2343) && (runs[0] <= 2657)));
+ Assert (rngName + " Runs length=2: " + runs[1], ((runs[1] >= 1135) && (runs[1] <= 1365)));
+ Assert (rngName + " Runs length=3: " + runs[2], ((runs[2] >= 542) && (runs[2] <= 708)));
+ Assert (rngName + " Runs length=4: " + runs[3], ((runs[3] >= 251) && (runs[3] <= 373)));
+ Assert (rngName + " Runs length=5: " + runs[4], ((runs[4] >= 111) && (runs[4] <= 201)));
+ Assert (rngName + " Runs length=6+ " + runs[5], ((runs[5] >= 111) && (runs[5] <= 201)));
+ }
+
+ // no long runs of 26 or more (0 or 1)
+ protected void LongRuns (string rngName, byte[] sample)
+ {
+ int longestRun = 0;
+ int currentRun = 0;
+ bool one = false;
+ bool zero = false;
+ for (int i = sample.Length - 1; i >= 0 ; i--) {
+ byte b = sample[i];
+ for (int j = 0; j < 8; j++) {
+ if ((b & 0x01) == 0x01) {
+ if (!one) {
+ one = true;
+ zero = false;
+ longestRun = Math.Max (longestRun, currentRun);
+ currentRun = 0;
+ }
+ currentRun++;
+ }
+ else {
+ if (!zero) {
+ one = false;
+ zero = true;
+ longestRun = Math.Max (longestRun, currentRun);
+ currentRun = 0;
+ }
+ currentRun++;
+ }
+ // next bit
+ b >>= 1;
+ }
+ }
+ Assert (rngName + " Long Runs max = " + longestRun, (longestRun < 26));
+ }
+
+ // all tests should be done on the same random sample
+ public void TestFIPS140()
+ {
+ string name = rng.ToString();
+ // 20,000 bits
+ byte[] sample = new byte[2500];
+ rng.GetBytes (sample);
+
+ Monobit (name, sample);
+ Poker (name, sample);
+ Runs (name, sample);
+ LongRuns (name, sample);
+ }
+}
+
+} \ No newline at end of file
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/SHA1CryptoServiceProviderTest.cs b/mcs/class/corlib/Test/System.Security.Cryptography/SHA1CryptoServiceProviderTest.cs
new file mode 100644
index 00000000000..6f2c8d8292a
--- /dev/null
+++ b/mcs/class/corlib/Test/System.Security.Cryptography/SHA1CryptoServiceProviderTest.cs
@@ -0,0 +1,92 @@
+//
+// SHA1CryptoServiceProviderTest.cs - NUnit Test Cases for SHA1CryptoServiceProvider
+//
+// Author:
+// Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using NUnit.Framework;
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MonoTests.System.Security.Cryptography {
+
+// References:
+// a. FIPS PUB 180-1: Secure Hash Standard
+// http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt
+
+// we inherit from SHA1Test because all SHA1 implementation must return the
+// same results (hence should run a common set of unit tests).
+public class SHA1CryptoServiceProviderTest : SHA1Test {
+ public SHA1CryptoServiceProviderTest () : base ("System.Security.Cryptography.SHA1CryptoServiceProvider testsuite") {}
+ public SHA1CryptoServiceProviderTest (string name) : base (name) {}
+
+ protected override void SetUp ()
+ {
+ hash = new SHA1CryptoServiceProvider ();
+ }
+
+ protected override void TearDown () {}
+
+ public static new ITest Suite
+ {
+ get {
+ return new TestSuite (typeof (SHA1CryptoServiceProviderTest));
+ }
+ }
+
+ public override void TestCreate ()
+ {
+ // no need to repeat this test
+ }
+
+ // none of those values changes for a particuliar implementation of SHA1
+ public override void TestStaticInfo ()
+ {
+ // test all values static for SHA1
+ base.TestStaticInfo();
+ string className = hash.ToString ();
+ AssertEquals (className + ".CanReuseTransform", true, hash.CanReuseTransform);
+ AssertEquals (className + ".CanTransformMultipleBlocks", true, hash.CanTransformMultipleBlocks);
+ AssertEquals (className + ".ToString()", "System.Security.Cryptography.SHA1CryptoServiceProvider", className);
+ }
+
+ public void TestSHA1CSPforFIPSCompliance ()
+ {
+ SHA1 sha = (SHA1)hash;
+ // First test, we hash the string "abc"
+ FIPS186_Test1 (sha);
+ // Second test, we hash the string "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+ FIPS186_Test2 (sha);
+ // Third test, we hash 1,000,000 times the character "a"
+ FIPS186_Test3 (sha);
+ }
+
+ // LAMESPEC / MSBUG: Under Windows an Initialize is required after
+ // TransformFinalBlock/Hash or SHA1CryptoServiceProvider will still return
+ // the previous Hash. SHA1Managed behavior's is different as it will return
+ // a bad Hash if Initialize isn't called.
+ // FIXME: Do we want to duplicate this bad behaviour ?
+/* public void TestInitialize ()
+ {
+ byte[] expectedDEF = { 0x58, 0x9c, 0x22, 0x33, 0x5a, 0x38, 0x1f, 0x12, 0x2d, 0x12, 0x92, 0x25, 0xf5, 0xc0, 0xba, 0x30, 0x56, 0xed, 0x58, 0x11 };
+ string className = hash.ToString ();
+ // hash abc
+ byte[] inputABC = Encoding.Default.GetBytes ("abc");
+ hash.TransformFinalBlock (inputABC, 0, inputABC.Length);
+ byte[] resultABC = hash.Hash;
+ // hash def
+ byte[] inputDEF = Encoding.Default.GetBytes ("def");
+ byte[] resultDEF = hash.ComputeHash (inputDEF);
+ // result(abc) == result(def) -> forgot to initialize
+ AssertEquals (className + ".Initialize ABC=DEF", resultABC, resultDEF);
+ hash.Initialize ();
+ resultDEF = hash.ComputeHash (inputDEF);
+ AssertEquals (className + ".Initialize DEF ok", expectedDEF, resultDEF);
+ }*/
+}
+
+}
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/SHA1Test.cs b/mcs/class/corlib/Test/System.Security.Cryptography/SHA1Test.cs
new file mode 100644
index 00000000000..60bbf8bfe17
--- /dev/null
+++ b/mcs/class/corlib/Test/System.Security.Cryptography/SHA1Test.cs
@@ -0,0 +1,210 @@
+//
+// SHA1Test.cs - NUnit Test Cases for SHA1
+//
+// Author:
+// Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using NUnit.Framework;
+using System;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MonoTests.System.Security.Cryptography
+{
+
+// References:
+// a. FIPS PUB 180-1: Secure Hash Standard
+// http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt
+
+// SHA1 is a abstract class - so most of the test included here wont be tested
+// on the abstract class but should be tested in ALL its descendants.
+public class SHA1Test : HashAlgorithmTest
+{
+ public SHA1Test () : base ("System.Security.Cryptography.SHA1 testsuite") {}
+ public SHA1Test (string name) : base (name) {}
+
+ protected override void SetUp ()
+ {
+ hash = SHA1.Create();
+ }
+
+ protected override void TearDown () {}
+
+ public static new ITest Suite {
+ get {
+ return new TestSuite (typeof (SHA1Test));
+ }
+ }
+
+ public void AssertEquals (string msg, byte[] array1, byte[] array2)
+ {
+ AllTests.AssertEquals (msg, array1, array2);
+ }
+
+ // test vectors from NIST FIPS 186-2
+
+ private string input1 = "abc";
+ private string input2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+ public void FIPS186_Test1 (SHA1 hash)
+ {
+ string className = hash.ToString ();
+ byte[] result = { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d };
+ byte[] input = Encoding.Default.GetBytes (input1);
+
+ string testName = className + " 1";
+ FIPS186_a (testName, hash, input, result);
+ FIPS186_b (testName, hash, input, result);
+ FIPS186_c (testName, hash, input, result);
+ FIPS186_d (testName, hash, input, result);
+ FIPS186_e (testName, hash, input, result);
+ }
+
+ public void FIPS186_Test2 (SHA1 hash)
+ {
+ string className = hash.ToString ();
+ byte[] result = { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 };
+ byte[] input = Encoding.Default.GetBytes (input2);
+
+ string testName = className + " 2";
+ FIPS186_a (testName, hash, input, result);
+ FIPS186_b (testName, hash, input, result);
+ FIPS186_c (testName, hash, input, result);
+ FIPS186_d (testName, hash, input, result);
+ FIPS186_e (testName, hash, input, result);
+ }
+
+ public void FIPS186_Test3 (SHA1 hash)
+ {
+ string className = hash.ToString ();
+ byte[] result = { 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f };
+ byte[] input = new byte [1000000];
+ for (int i = 0; i < 1000000; i++)
+ input[i] = 0x61; // a
+
+ string testName = className + " 3";
+ FIPS186_a (testName, hash, input, result);
+ FIPS186_b (testName, hash, input, result);
+ FIPS186_c (testName, hash, input, result);
+ FIPS186_d (testName, hash, input, result);
+ FIPS186_e (testName, hash, input, result);
+ }
+
+ public void FIPS186_a (string testName, SHA1 hash, byte[] input, byte[] result)
+ {
+ byte[] output = hash.ComputeHash (input);
+ AssertEquals (testName + ".a.1", result, output);
+ AssertEquals (testName + ".a.2", result, hash.Hash);
+ // required or next operation will still return old hash
+ hash.Initialize ();
+ }
+
+ public void FIPS186_b (string testName, SHA1 hash, byte[] input, byte[] result)
+ {
+ byte[] output = hash.ComputeHash (input, 0, input.Length);
+ AssertEquals (testName + ".b.1", result, output);
+ AssertEquals (testName + ".b.2", result, hash.Hash);
+ // required or next operation will still return old hash
+ hash.Initialize ();
+ }
+
+ public void FIPS186_c (string testName, SHA1 hash, byte[] input, byte[] result)
+ {
+ MemoryStream ms = new MemoryStream (input);
+ byte[] output = hash.ComputeHash (ms);
+ AssertEquals (testName + ".c.1", result, output);
+ AssertEquals (testName + ".c.2", result, hash.Hash);
+ // required or next operation will still return old hash
+ hash.Initialize ();
+ }
+
+ public void FIPS186_d (string testName, SHA1 hash, byte[] input, byte[] result)
+ {
+ byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
+ // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
+ // AssertEquals( testName + ".d.1", result, output );
+ AssertEquals (testName + ".d", result, hash.Hash);
+ // required or next operation will still return old hash
+ hash.Initialize ();
+ }
+
+ public void FIPS186_e (string testName, SHA1 hash, byte[] input, byte[] result)
+ {
+ byte[] copy = new byte [input.Length];
+ for (int i=0; i < input.Length - 1; i++)
+ hash.TransformBlock (input, i, 1, copy, i);
+ byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
+ // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
+ // AssertEquals (testName + ".e.1", result, output);
+ AssertEquals (testName + ".e", result, hash.Hash);
+ // required or next operation will still return old hash
+ hash.Initialize ();
+ }
+
+ public override void TestCreate ()
+ {
+ // Note: These tests will only be valid without a "machine.config" file
+ // or a "machine.config" file that do not modify the default algorithm
+ // configuration.
+ const string defaultSHA1 = "System.Security.Cryptography.SHA1CryptoServiceProvider";
+
+ // try to build the default implementation
+ SHA1 hash = SHA1.Create ();
+ AssertEquals ("SHA1.Create()", hash.ToString (), defaultSHA1);
+
+ // try to build, in every way, a SHA1 implementation
+ // note that it's not possible to create an instance of SHA1Managed using the Create methods
+ hash = SHA1.Create ("SHA");
+ AssertEquals ("SHA1.Create('SHA')", hash.ToString (), defaultSHA1);
+ hash = SHA1.Create ("SHA1");
+ AssertEquals ("SHA1.Create('SHA1')", hash.ToString (), defaultSHA1);
+ hash = SHA1.Create ("System.Security.Cryptography.SHA1");
+ AssertEquals ("SHA1.Create('System.Security.Cryptography.SHA1')", hash.ToString (), defaultSHA1);
+ hash = SHA1.Create ("System.Security.Cryptography.HashAlgorithm" );
+ AssertEquals ("SHA1.Create('System.Security.Cryptography.HashAlgorithm')", hash.ToString (), defaultSHA1);
+
+ // try to build an incorrect hash algorithms
+ try {
+ hash = SHA1.Create ("MD5");
+ Fail ("SHA1.Create('MD5') should throw InvalidCastException");
+ }
+ catch (InvalidCastException) {
+ // do nothing, this is what we expect
+ }
+ catch (Exception e) {
+ Fail ("SHA1.Create(null) should throw InvalidCastException not " + e.ToString ());
+ }
+
+ // try to build invalid implementation
+ hash = SHA1.Create ("InvalidHash");
+ AssertNull ("SHA1.Create('InvalidHash')", hash);
+
+ // try to build null implementation
+ try {
+ hash = SHA1.Create (null);
+ Fail ("SHA1.Create(null) should throw ArgumentNullException");
+ }
+ catch (ArgumentNullException) {
+ // do nothing, this is what we expect
+ }
+ catch (Exception e) {
+ Fail ("SHA1.Create(null) should throw ArgumentNullException not " + e.ToString ());
+ }
+ }
+
+ // none of those values changes for any implementation of SHA1
+ public virtual void TestStaticInfo ()
+ {
+ string className = hash.ToString ();
+ AssertEquals (className + ".HashSize", 160, hash.HashSize);
+ AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
+ AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
+ }
+
+}
+
+}