MD5Test.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // System.Security.Cryptography.Test MD5 NUnit test classes.
  3. //
  4. // Authors:
  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 MD5HashingTest : TestCase {
  15. byte[] _dataToHash;
  16. byte[] _expectedHash;
  17. public MD5HashingTest (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. MD5 md5 = new MD5CryptoServiceProvider ();
  33. byte[] hash;
  34. hash = md5.ComputeHash (_dataToHash);
  35. Assert (ArrayEquals (hash, _expectedHash));
  36. }
  37. }
  38. public class MD5TestSet {
  39. public MD5TestSet () {
  40. }
  41. public static void Main () {
  42. }
  43. public static ITest Suite {
  44. get {
  45. TestSuite suite = new TestSuite ();
  46. byte[] hash0 = {0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E};
  47. suite.AddTest (new MD5HashingTest ("(blank hash)", new byte[0], hash0));
  48. byte[] hash1 = {0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61};
  49. suite.AddTest (new MD5HashingTest ("a", Encoding.UTF8.GetBytes("a"), hash1));
  50. byte[] hash2 = {0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72};
  51. suite.AddTest (new MD5HashingTest ("abc", Encoding.UTF8.GetBytes("abc"), hash2));
  52. byte[] hash3 = {0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B};
  53. suite.AddTest (new MD5HashingTest ("abcdefghijklmnopqrstuvwxyz", Encoding.UTF8.GetBytes("abcdefghijklmnopqrstuvwxyz"), hash3));
  54. byte[] hash4 = {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f};
  55. suite.AddTest (new MD5HashingTest ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), hash4));
  56. return suite;
  57. }
  58. }
  59. }
  60. }