digest.hpp 887 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_DETAIL_DIGEST_HPP
  10. #define BOOST_JSON_DETAIL_DIGEST_HPP
  11. BOOST_JSON_NS_BEGIN
  12. namespace detail {
  13. // Calculate salted digest of string
  14. inline
  15. std::size_t
  16. digest(
  17. char const* s,
  18. std::size_t n,
  19. std::size_t salt) noexcept
  20. {
  21. #if BOOST_JSON_ARCH == 64
  22. std::uint64_t const prime = 0x100000001B3ULL;
  23. std::uint64_t hash = 0xcbf29ce484222325ULL;
  24. #else
  25. std::uint32_t const prime = 0x01000193UL;
  26. std::uint32_t hash = 0x811C9DC5UL;
  27. #endif
  28. hash += salt;
  29. for(;n--;++s)
  30. hash = (*s ^ hash) * prime;
  31. return hash;
  32. }
  33. } // detail
  34. BOOST_JSON_NS_END
  35. #endif