hash.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <apt-pkg/md5.h>
  2. #include <apt-pkg/sha1.h>
  3. #include <apt-pkg/sha256.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <iostream>
  6. using namespace std;
  7. template <class T> void Test(const char *In,const char *Out)
  8. {
  9. T Sum;
  10. Sum.Add(In);
  11. cout << Sum.Result().Value() << endl;
  12. if (stringcasecmp(Sum.Result().Value(),Out) != 0)
  13. abort();
  14. }
  15. template <class T> void TestMill(const char *Out)
  16. {
  17. T Sum;
  18. const unsigned char As[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  19. unsigned Count = 1000000;
  20. for (; Count != 0;)
  21. {
  22. if (Count >= 64)
  23. {
  24. Sum.Add(As,64);
  25. Count -= 64;
  26. }
  27. else
  28. {
  29. Sum.Add(As,Count);
  30. Count = 0;
  31. }
  32. }
  33. cout << Sum.Result().Value() << endl;
  34. if (stringcasecmp(Sum.Result().Value(),Out) != 0)
  35. abort();
  36. }
  37. int main()
  38. {
  39. // From FIPS PUB 180-1
  40. Test<SHA1Summation>("abc","A9993E364706816ABA3E25717850C26C9CD0D89D");
  41. Test<SHA1Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  42. "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");
  43. TestMill<SHA1Summation>("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F");
  44. // MD5 tests from RFC 1321
  45. Test<MD5Summation>("","d41d8cd98f00b204e9800998ecf8427e");
  46. Test<MD5Summation>("a","0cc175b9c0f1b6a831c399e269772661");
  47. Test<MD5Summation>("abc","900150983cd24fb0d6963f7d28e17f72");
  48. Test<MD5Summation>("message digest","f96b697d7cb7938d525a2f31aaf161d0");
  49. Test<MD5Summation>("abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b");
  50. Test<MD5Summation>("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  51. "d174ab98d277d9f5a5611c2c9f419d9f");
  52. Test<MD5Summation>("12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  53. "57edf4a22be3c955ac49da2e2107b67a");
  54. // SHA-256, From FIPS 180-2
  55. Test<SHA256Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  56. "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
  57. return 0;
  58. }