hash.h 3.0 KB

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