test_md5.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // libs/uuid/test/test_md5.cpp --------------------------------//
  2. // (C) Copyright 2017 - 2019 James E. King III
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // https://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/cstdint.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/uuid/detail/md5.hpp>
  9. #if !(defined(BOOST_UUID_COMPAT_PRE_1_71_MD5) && BOOST_ENDIAN_LITTLE_BYTE)
  10. #include "digestutils.hpp"
  11. #endif
  12. #define BOOST_TEST_MD5_DIGEST(lhs, rhs) \
  13. ( boost::uuids::test::test_digest_equal_array(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION, (lhs), (rhs), 16) )
  14. int main(int, char**)
  15. {
  16. typedef struct
  17. {
  18. const char * data;
  19. boost::uint32_t len;
  20. unsigned char expected[16];
  21. } Expectation;
  22. /* http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/ */
  23. Expectation expectations[3] = {
  24. { "The quick brown fox jumps over the lazy dog", 43,
  25. { 0x9e, 0x10, 0x7d, 0x9d, 0x37, 0x2b, 0xb6, 0x82,
  26. 0x6b, 0xd8, 0x1d, 0x35, 0x42, 0xa4, 0x19, 0xd6 }},
  27. { "Test vector from febooti.com", 28,
  28. { 0x50, 0x0a, 0xb6, 0x61, 0x3c, 0x6d, 0xb7, 0xfb,
  29. 0xd3, 0x0c, 0x62, 0xf5, 0xff, 0x57, 0x3d, 0x0f }},
  30. { "", 0,
  31. { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
  32. 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e }}
  33. };
  34. for (boost::uint32_t i = 0; i < 3; ++i) {
  35. boost::uuids::detail::md5 hash;
  36. hash.process_bytes(expectations[i].data, expectations[i].len);
  37. boost::uuids::detail::md5::digest_type result;
  38. hash.get_digest(result);
  39. #if defined(BOOST_UUID_COMPAT_PRE_1_71_MD5) && BOOST_ENDIAN_LITTLE_BYTE
  40. // this is the original, incorrect behavior from pre-1.71
  41. BOOST_TEST_EQ(0, memcmp(result, expectations[i].expected,
  42. sizeof(boost::uuids::detail::md5::digest_type)));
  43. #else
  44. unsigned char raw_result[16];
  45. boost::uuids::test::copy_raw_digest(raw_result, result, 4);
  46. BOOST_TEST_MD5_DIGEST(raw_result, expectations[i].expected);
  47. #endif
  48. BOOST_TEST_EQ(hash.get_version(), 0x03);
  49. }
  50. return boost::report_errors();
  51. }