2 // SHA1CryptoServiceProviderTest.cs - NUnit Test Cases for SHA1CryptoServiceProvider
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
11 using NUnit.Framework;
13 using System.Security.Cryptography;
16 namespace MonoTests.System.Security.Cryptography {
19 // a. FIPS PUB 180-1: Secure Hash Standard
20 // http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt
22 // we inherit from SHA1Test because all SHA1 implementation must return the
23 // same results (hence should run a common set of unit tests).
26 public class SHA1CryptoServiceProviderTest : SHA1Test {
29 protected override void SetUp ()
31 hash = new SHA1CryptoServiceProvider ();
35 public override void Create ()
37 // no need to repeat this test
40 // none of those values changes for a particuliar implementation of SHA1
42 public override void StaticInfo ()
44 // test all values static for SHA1
46 string className = hash.ToString ();
47 Assert.IsTrue (hash.CanReuseTransform, className + ".CanReuseTransform");
48 Assert.IsTrue (hash.CanTransformMultipleBlocks, className + ".CanTransformMultipleBlocks");
49 Assert.AreEqual ("System.Security.Cryptography.SHA1CryptoServiceProvider", className, className + ".ToString()");
52 public void TestSHA1CSPforFIPSCompliance ()
54 SHA1 sha = (SHA1)hash;
55 // First test, we hash the string "abc"
57 // Second test, we hash the string "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
59 // Third test, we hash 1,000,000 times the character "a"
63 // LAMESPEC / MSBUG: Under Windows an Initialize is required after
64 // TransformFinalBlock/Hash or SHA1CryptoServiceProvider will still return
65 // the previous Hash. SHA1Managed behavior's is different as it will return
66 // a bad Hash if Initialize isn't called.
67 // FIXME: Do we want to duplicate this bad behaviour ?
68 /* public void TestInitialize ()
70 byte[] expectedDEF = { 0x58, 0x9c, 0x22, 0x33, 0x5a, 0x38, 0x1f, 0x12, 0x2d, 0x12, 0x92, 0x25, 0xf5, 0xc0, 0xba, 0x30, 0x56, 0xed, 0x58, 0x11 };
71 string className = hash.ToString ();
73 byte[] inputABC = Encoding.Default.GetBytes ("abc");
74 hash.TransformFinalBlock (inputABC, 0, inputABC.Length);
75 byte[] resultABC = hash.Hash;
77 byte[] inputDEF = Encoding.Default.GetBytes ("def");
78 byte[] resultDEF = hash.ComputeHash (inputDEF);
79 // result(abc) == result(def) -> forgot to initialize
80 AssertEquals (className + ".Initialize ABC=DEF", resultABC, resultDEF);
82 resultDEF = hash.ComputeHash (inputDEF);
83 AssertEquals (className + ".Initialize DEF ok", expectedDEF, resultDEF);