1 // Copyright 2018 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_TOKEN_H_ 6 #define BASE_TOKEN_H_ 7 8 #include <stdint.h> 9 10 #include <array> 11 #include <compare> 12 #include <optional> 13 #include <string> 14 #include <string_view> 15 16 #include "base/base_export.h" 17 #include "base/containers/span.h" 18 19 namespace base { 20 21 // A Token is a randomly chosen 128-bit integer. This class supports generation 22 // from a cryptographically strong random source, or constexpr construction over 23 // fixed values (e.g. to store a pre-generated constant value). Tokens are 24 // similar in spirit and purpose to UUIDs, without many of the constraints and 25 // expectations (such as byte layout and string representation) clasically 26 // associated with UUIDs. 27 class BASE_EXPORT Token { 28 public: 29 // Constructs a zero Token. 30 constexpr Token() = default; 31 32 // Constructs a Token with |high| and |low| as its contents. Token(uint64_t high,uint64_t low)33 constexpr Token(uint64_t high, uint64_t low) : words_{high, low} {} 34 35 constexpr Token(const Token&) = default; 36 constexpr Token& operator=(const Token&) = default; 37 constexpr Token(Token&&) noexcept = default; 38 constexpr Token& operator=(Token&&) = default; 39 40 // Constructs a new Token with random |high| and |low| values taken from a 41 // cryptographically strong random source. The result's |is_zero()| is 42 // guaranteed to be false. 43 static Token CreateRandom(); 44 45 // The high and low 64 bits of this Token. high()46 constexpr uint64_t high() const { return words_[0]; } low()47 constexpr uint64_t low() const { return words_[1]; } 48 is_zero()49 constexpr bool is_zero() const { return words_[0] == 0 && words_[1] == 0; } 50 AsBytes()51 span<const uint8_t, 16> AsBytes() const { return as_byte_span(words_); } 52 53 friend constexpr auto operator<=>(const Token& lhs, 54 const Token& rhs) = default; 55 friend constexpr bool operator==(const Token& lhs, 56 const Token& rhs) = default; 57 58 // Generates a string representation of this Token useful for e.g. logging. 59 std::string ToString() const; 60 61 // FromString is the opposite of ToString. It returns std::nullopt if the 62 // |string_representation| is invalid. 63 static std::optional<Token> FromString( 64 std::string_view string_representation); 65 66 private: 67 // Note: Two uint64_t are used instead of uint8_t[16] in order to have a 68 // simpler implementation, particularly for |ToString()|, |is_zero()|, and 69 // constexpr value construction. 70 71 std::array<uint64_t, 2> words_ = {0, 0}; 72 }; 73 74 // For use in std::unordered_map. 75 struct BASE_EXPORT TokenHash { 76 size_t operator()(const Token& token) const; 77 }; 78 79 class Pickle; 80 class PickleIterator; 81 82 // For serializing and deserializing Token values. 83 BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token); 84 BASE_EXPORT std::optional<Token> ReadTokenFromPickle( 85 PickleIterator* pickle_iterator); 86 87 } // namespace base 88 89 #endif // BASE_TOKEN_H_ 90