hash.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_HASH_HASH_H_
  5. #define BASE_HASH_HASH_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <limits>
  9. #include <string>
  10. #include <utility>
  11. #include "base/base_export.h"
  12. #include "base/containers/span.h"
  13. #include "base/strings/string16.h"
  14. #include "base/strings/string_piece.h"
  15. namespace base {
  16. // WARNING: This hash functions should not be used for any cryptographic
  17. // purpose.
  18. // Deprecated: Computes a hash of a memory buffer, use FastHash() instead.
  19. // If you need to persist a change on disk or between computers, use
  20. // PersistentHash().
  21. // TODO(https://crbug.com/1025358): Migrate client code to new hash function.
  22. BASE_EXPORT uint32_t Hash(const void* data, size_t length);
  23. BASE_EXPORT uint32_t Hash(const std::string& str);
  24. BASE_EXPORT uint32_t Hash(const string16& str);
  25. // Really *fast* and high quality hash.
  26. // Recommended hash function for general use, we pick the best performant
  27. // hash for each build target.
  28. // It is prone to be updated whenever a newer/faster hash function is
  29. // publicly available.
  30. // May changed without warning, do not expect stability of outputs.
  31. BASE_EXPORT size_t FastHash(base::span<const uint8_t> data);
  32. inline size_t FastHash(StringPiece str) {
  33. return FastHash(as_bytes(make_span(str)));
  34. }
  35. // Computes a hash of a memory buffer. This hash function must not change so
  36. // that code can use the hashed values for persistent storage purposes or
  37. // sending across the network. If a new persistent hash function is desired, a
  38. // new version will have to be added in addition.
  39. //
  40. // WARNING: This hash function should not be used for any cryptographic purpose.
  41. BASE_EXPORT uint32_t PersistentHash(base::span<const uint8_t> data);
  42. BASE_EXPORT uint32_t PersistentHash(const void* data, size_t length);
  43. BASE_EXPORT uint32_t PersistentHash(const std::string& str);
  44. // Hash pairs of 32-bit or 64-bit numbers.
  45. BASE_EXPORT size_t HashInts32(uint32_t value1, uint32_t value2);
  46. BASE_EXPORT size_t HashInts64(uint64_t value1, uint64_t value2);
  47. template <typename T1, typename T2>
  48. inline size_t HashInts(T1 value1, T2 value2) {
  49. // This condition is expected to be compile-time evaluated and optimised away
  50. // in release builds.
  51. if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t)))
  52. return HashInts64(value1, value2);
  53. return HashInts32(static_cast<uint32_t>(value1),
  54. static_cast<uint32_t>(value2));
  55. }
  56. // A templated hasher for pairs of integer types. Example:
  57. //
  58. // using MyPair = std::pair<int32_t, int32_t>;
  59. // std::unordered_set<MyPair, base::IntPairHash<MyPair>> set;
  60. template <typename T>
  61. struct IntPairHash;
  62. template <typename Type1, typename Type2>
  63. struct IntPairHash<std::pair<Type1, Type2>> {
  64. size_t operator()(std::pair<Type1, Type2> value) const {
  65. return HashInts(value.first, value.second);
  66. }
  67. };
  68. } // namespace base
  69. #endif // BASE_HASH_HASH_H_