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