hash.cc 1.7 KB

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