token.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018 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_TOKEN_H_
  5. #define BASE_TOKEN_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <tuple>
  9. #include "base/base_export.h"
  10. #include "base/hash/hash.h"
  11. #include "base/optional.h"
  12. namespace base {
  13. // A Token is a randomly chosen 128-bit integer. This class supports generation
  14. // from a cryptographically strong random source, or constexpr construction over
  15. // fixed values (e.g. to store a pre-generated constant value). Tokens are
  16. // similar in spirit and purpose to UUIDs, without many of the constraints and
  17. // expectations (such as byte layout and string representation) clasically
  18. // associated with UUIDs.
  19. class BASE_EXPORT Token {
  20. public:
  21. // Constructs a zero Token.
  22. constexpr Token() = default;
  23. // Constructs a Token with |high| and |low| as its contents.
  24. constexpr Token(uint64_t high, uint64_t low) : high_(high), low_(low) {}
  25. constexpr Token(const Token&) = default;
  26. constexpr Token& operator=(const Token&) = default;
  27. constexpr Token(Token&&) noexcept = default;
  28. constexpr Token& operator=(Token&&) = default;
  29. // Constructs a new Token with random |high| and |low| values taken from a
  30. // cryptographically strong random source.
  31. static Token CreateRandom();
  32. // The high and low 64 bits of this Token.
  33. constexpr uint64_t high() const { return high_; }
  34. constexpr uint64_t low() const { return low_; }
  35. constexpr bool is_zero() const { return high_ == 0 && low_ == 0; }
  36. constexpr bool operator==(const Token& other) const {
  37. return high_ == other.high_ && low_ == other.low_;
  38. }
  39. constexpr bool operator!=(const Token& other) const {
  40. return !(*this == other);
  41. }
  42. constexpr bool operator<(const Token& other) const {
  43. return std::tie(high_, low_) < std::tie(other.high_, other.low_);
  44. }
  45. // Generates a string representation of this Token useful for e.g. logging.
  46. std::string ToString() const;
  47. private:
  48. // Note: Two uint64_t are used instead of uint8_t[16] in order to have a
  49. // simpler implementation, paricularly for |ToString()|, |is_zero()|, and
  50. // constexpr value construction.
  51. uint64_t high_ = 0;
  52. uint64_t low_ = 0;
  53. };
  54. // For use in std::unordered_map.
  55. struct TokenHash {
  56. size_t operator()(const base::Token& token) const {
  57. return base::HashInts64(token.high(), token.low());
  58. }
  59. };
  60. class Pickle;
  61. class PickleIterator;
  62. // For serializing and deserializing Token values.
  63. BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token);
  64. BASE_EXPORT Optional<Token> ReadTokenFromPickle(
  65. PickleIterator* pickle_iterator);
  66. } // namespace base
  67. #endif // BASE_TOKEN_H_