SHA256Test.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // System.Security.Cryptography.Test SHA256 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 SHA256HashingTest : TestCase {
  15. byte[] _dataToHash;
  16. byte[] _expectedHash;
  17. public SHA256HashingTest (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. SHA256 sha = new SHA256Managed ();
  33. byte[] hash;
  34. hash = sha.ComputeHash (_dataToHash);
  35. Assert (ArrayEquals (hash, _expectedHash));
  36. }
  37. }
  38. public class SHA256TestSet {
  39. public SHA256TestSet () {
  40. }
  41. public static void Main () {
  42. }
  43. public static ITest Suite {
  44. get {
  45. TestSuite suite = new TestSuite ();
  46. byte[] hash0 = {0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD};
  47. suite.AddTest (new SHA256HashingTest ("abc", Encoding.UTF8.GetBytes("abc"), hash0));
  48. byte[] hash1 = {0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1};
  49. suite.AddTest (new SHA256HashingTest ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", Encoding.UTF8.GetBytes("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"), hash1));
  50. //byte[] hash2 = {};
  51. //suite.AddTest (new SHA256HashingTest ("abc", Encoding.UTF8.GetBytes("abc"), hash2));
  52. //byte[] hash3 = {};
  53. //suite.AddTest (new SHA256HashingTest ("abcdefghijklmnopqrstuvwxyz", Encoding.UTF8.GetBytes("abcdefghijklmnopqrstuvwxyz"), hash3));
  54. //byte[] hash4 = {};
  55. //suite.AddTest (new SHA256HashingTest ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", Encoding.UTF8.GetBytes("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"), hash4));
  56. return suite;
  57. }
  58. }
  59. }
  60. }