tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hash.h (2951B)


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