MD5.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * This code implements the MD5 message-digest algorithm.
  3. * The algorithm is due to Ron Rivest. This code was
  4. * written by Colin Plumb in 1993, no copyright is claimed.
  5. * This code is in the public domain; do with it what you wish.
  6. *
  7. * Equivalent code is available from RSA Data Security, Inc.
  8. * This code has been tested against that, and is equivalent,
  9. * except that you don't need to include two pages of legalese
  10. * with every copy.
  11. */
  12. #include "config.h"
  13. #include <wtf/MD5.h>
  14. #include <wtf/StringExtras.h>
  15. #include <wtf/text/CString.h>
  16. namespace TestWebKitAPI {
  17. static void expectMD5(CString input, CString expected)
  18. {
  19. MD5 md5;
  20. md5.addBytes(reinterpret_cast<const uint8_t*>(input.data()), input.length());
  21. Vector<uint8_t, 16> digest;
  22. md5.checksum(digest);
  23. char* buf = 0;
  24. CString actual = CString::newUninitialized(32, buf);
  25. for (size_t i = 0; i < 16; i++, buf += 2)
  26. snprintf(buf, 3, "%02x", digest.at(i));
  27. ASSERT_EQ(expected.length(), actual.length());
  28. ASSERT_STREQ(expected.data(), actual.data());
  29. }
  30. TEST(WTF_MD5, Computation)
  31. {
  32. // MD5 Test suite from http://www.ietf.org/rfc/rfc1321.txt.
  33. expectMD5("", "d41d8cd98f00b204e9800998ecf8427e");
  34. expectMD5("a", "0cc175b9c0f1b6a831c399e269772661");
  35. expectMD5("abc", "900150983cd24fb0d6963f7d28e17f72");
  36. expectMD5("message digest", "f96b697d7cb7938d525a2f31aaf161d0");
  37. expectMD5("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
  38. expectMD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f");
  39. expectMD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a");
  40. }
  41. } // namespace TestWebKitAPI