SHA1Test.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // System.Security.Cryptography.Test SHA1 NUnit test classes.
  3. //
  4. // Author:
  5. // Matthew S. Ford ([email protected])
  6. //
  7. // Copyright 2001 by Matthew S. Ford.
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. namespace System.Security.Cryptography.Test {
  14. public class SHA1HashingTest : TestCase {
  15. byte[] _dataToHash;
  16. byte[] _expectedHash;
  17. public SHA1HashingTest (string name, byte[] dataToHash, byte[] expectedHash) : base(name) {
  18. _dataToHash = dataToHash;
  19. _expectedHash = expectedHash;
  20. }
  21. public static bool ArrayEquals (byte[] arr1, byte[] arr2) {
  22. int i;
  23. if (arr1.GetLength(0) != arr2.GetLength(0))
  24. return false;
  25. for (i=0; i<arr1.GetLength(0); i++) {
  26. if (arr1[i] != arr2[i])
  27. return false;
  28. }
  29. return true;
  30. }
  31. protected override void RunTest () {
  32. SHA1 sha = new SHA1CryptoServiceProvider ();
  33. byte[] hash;
  34. hash = sha.ComputeHash (_dataToHash);
  35. Assert (ArrayEquals (hash, _expectedHash));
  36. }
  37. }
  38. public class SHA1TestSet {
  39. public SHA1TestSet () {
  40. }
  41. public static void Main () {
  42. }
  43. public static ITest Suite {
  44. get {
  45. TestSuite suite = new TestSuite ();
  46. byte[] hash0 = {0xDA, 0x39, 0xA3, 0xEE, 0x5E, 0x6B, 0x4B, 0x0D, 0x32, 0x55, 0xBF, 0xEF, 0x95, 0x60, 0x18, 0x90, 0xAF, 0xD8, 0x07, 0x09};
  47. suite.AddTest (new SHA1HashingTest ("(blank hash)", new byte[0], hash0));
  48. byte[] hash1 = {0x86, 0xF7, 0xE4, 0x37, 0xFA, 0xA5, 0xA7, 0xFC, 0xE1, 0x5D, 0x1D, 0xDC, 0xB9, 0xEA, 0xEA, 0xEA, 0x37, 0x76, 0x67, 0xB8};
  49. suite.AddTest (new SHA1HashingTest ("a", Encoding.UTF8.GetBytes("a"), hash1));
  50. byte[] hash2 = {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D};
  51. suite.AddTest (new SHA1HashingTest ("abc", Encoding.UTF8.GetBytes("abc"), hash2));
  52. byte[] hash3 = {0x32, 0xD1, 0x0C, 0x7B, 0x8C, 0xF9, 0x65, 0x70, 0xCA, 0x04, 0xCE, 0x37, 0xF2, 0xA1, 0x9D, 0x84, 0x24, 0x0D, 0x3A, 0x89};
  53. suite.AddTest (new SHA1HashingTest ("abcdefghijklmnopqrstuvwxyz", Encoding.UTF8.GetBytes("abcdefghijklmnopqrstuvwxyz"), hash3));
  54. byte[] hash4 = {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1};
  55. suite.AddTest (new SHA1HashingTest ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", Encoding.UTF8.GetBytes("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"), hash4));
  56. return suite;
  57. }
  58. }
  59. }
  60. }