digest.hpp 963 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  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. namespace boost {
  12. namespace json {
  13. namespace detail {
  14. // Calculate salted digest of string
  15. template<class ForwardIterator>
  16. std::size_t
  17. digest(
  18. ForwardIterator b,
  19. ForwardIterator e,
  20. std::size_t salt) noexcept
  21. {
  22. #if BOOST_JSON_ARCH == 64
  23. std::uint64_t const prime = 0x100000001B3ULL;
  24. std::uint64_t hash = 0xcbf29ce484222325ULL;
  25. #else
  26. std::uint32_t const prime = 0x01000193UL;
  27. std::uint32_t hash = 0x811C9DC5UL;
  28. #endif
  29. hash += salt;
  30. for(; b != e; ++b)
  31. hash = (*b ^ hash) * prime;
  32. return hash;
  33. }
  34. } // detail
  35. } // namespace json
  36. } // namespace boost
  37. #endif