SHA1CryptoServiceProviderTest.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // SHA1CryptoServiceProviderTest.cs - NUnit Test Cases for SHA1CryptoServiceProvider
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. namespace MonoTests.System.Security.Cryptography {
  14. // References:
  15. // a. FIPS PUB 180-1: Secure Hash Standard
  16. // http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt
  17. // we inherit from SHA1Test because all SHA1 implementation must return the
  18. // same results (hence should run a common set of unit tests).
  19. public class SHA1CryptoServiceProviderTest : SHA1Test {
  20. protected override void SetUp ()
  21. {
  22. hash = new SHA1CryptoServiceProvider ();
  23. }
  24. protected override void TearDown () {}
  25. public override void TestCreate ()
  26. {
  27. // no need to repeat this test
  28. }
  29. // none of those values changes for a particuliar implementation of SHA1
  30. public override void TestStaticInfo ()
  31. {
  32. // test all values static for SHA1
  33. base.TestStaticInfo();
  34. string className = hash.ToString ();
  35. AssertEquals (className + ".CanReuseTransform", true, hash.CanReuseTransform);
  36. AssertEquals (className + ".CanTransformMultipleBlocks", true, hash.CanTransformMultipleBlocks);
  37. AssertEquals (className + ".ToString()", "System.Security.Cryptography.SHA1CryptoServiceProvider", className);
  38. }
  39. public void TestSHA1CSPforFIPSCompliance ()
  40. {
  41. SHA1 sha = (SHA1)hash;
  42. // First test, we hash the string "abc"
  43. FIPS186_Test1 (sha);
  44. // Second test, we hash the string "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  45. FIPS186_Test2 (sha);
  46. // Third test, we hash 1,000,000 times the character "a"
  47. FIPS186_Test3 (sha);
  48. }
  49. // LAMESPEC / MSBUG: Under Windows an Initialize is required after
  50. // TransformFinalBlock/Hash or SHA1CryptoServiceProvider will still return
  51. // the previous Hash. SHA1Managed behavior's is different as it will return
  52. // a bad Hash if Initialize isn't called.
  53. // FIXME: Do we want to duplicate this bad behaviour ?
  54. /* public void TestInitialize ()
  55. {
  56. byte[] expectedDEF = { 0x58, 0x9c, 0x22, 0x33, 0x5a, 0x38, 0x1f, 0x12, 0x2d, 0x12, 0x92, 0x25, 0xf5, 0xc0, 0xba, 0x30, 0x56, 0xed, 0x58, 0x11 };
  57. string className = hash.ToString ();
  58. // hash abc
  59. byte[] inputABC = Encoding.Default.GetBytes ("abc");
  60. hash.TransformFinalBlock (inputABC, 0, inputABC.Length);
  61. byte[] resultABC = hash.Hash;
  62. // hash def
  63. byte[] inputDEF = Encoding.Default.GetBytes ("def");
  64. byte[] resultDEF = hash.ComputeHash (inputDEF);
  65. // result(abc) == result(def) -> forgot to initialize
  66. AssertEquals (className + ".Initialize ABC=DEF", resultABC, resultDEF);
  67. hash.Initialize ();
  68. resultDEF = hash.ComputeHash (inputDEF);
  69. AssertEquals (className + ".Initialize DEF ok", expectedDEF, resultDEF);
  70. }*/
  71. }
  72. }