hash.cc 1.8 KB

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