Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2018 The Chromium Authors |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 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 | |
Victor Costan | 57f635e | 2020-07-08 04:08:22 | [diff] [blame] | 10 | #include <string> |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 11 | #include <tuple> |
| 12 | |
| 13 | #include "base/base_export.h" |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 14 | #include "base/containers/span.h" |
Daniel Cheng | c058199 | 2019-03-29 04:52:56 | [diff] [blame] | 15 | #include "base/hash/hash.h" |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 16 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | // A Token is a randomly chosen 128-bit integer. This class supports generation |
| 21 | // from a cryptographically strong random source, or constexpr construction over |
| 22 | // fixed values (e.g. to store a pre-generated constant value). Tokens are |
| 23 | // similar in spirit and purpose to UUIDs, without many of the constraints and |
| 24 | // expectations (such as byte layout and string representation) clasically |
| 25 | // associated with UUIDs. |
| 26 | class BASE_EXPORT Token { |
| 27 | public: |
| 28 | // Constructs a zero Token. |
Victor Costan | 57f635e | 2020-07-08 04:08:22 | [diff] [blame] | 29 | constexpr Token() = default; |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 30 | |
| 31 | // Constructs a Token with |high| and |low| as its contents. |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 32 | constexpr Token(uint64_t high, uint64_t low) : words_{high, low} {} |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 33 | |
Victor Costan | 57f635e | 2020-07-08 04:08:22 | [diff] [blame] | 34 | constexpr Token(const Token&) = default; |
| 35 | constexpr Token& operator=(const Token&) = default; |
| 36 | constexpr Token(Token&&) noexcept = default; |
| 37 | constexpr Token& operator=(Token&&) = default; |
| 38 | |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 39 | // Constructs a new Token with random |high| and |low| values taken from a |
| 40 | // cryptographically strong random source. |
| 41 | static Token CreateRandom(); |
| 42 | |
| 43 | // The high and low 64 bits of this Token. |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 44 | constexpr uint64_t high() const { return words_[0]; } |
| 45 | constexpr uint64_t low() const { return words_[1]; } |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 46 | |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 47 | constexpr bool is_zero() const { return words_[0] == 0 && words_[1] == 0; } |
| 48 | |
| 49 | span<const uint8_t, 16> AsBytes() const { |
| 50 | return as_bytes(make_span(words_)); |
| 51 | } |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 52 | |
Victor Costan | 57f635e | 2020-07-08 04:08:22 | [diff] [blame] | 53 | constexpr bool operator==(const Token& other) const { |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 54 | return words_[0] == other.words_[0] && words_[1] == other.words_[1]; |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 55 | } |
| 56 | |
Victor Costan | 57f635e | 2020-07-08 04:08:22 | [diff] [blame] | 57 | constexpr bool operator!=(const Token& other) const { |
| 58 | return !(*this == other); |
| 59 | } |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 60 | |
Victor Costan | 57f635e | 2020-07-08 04:08:22 | [diff] [blame] | 61 | constexpr bool operator<(const Token& other) const { |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 62 | return std::tie(words_[0], words_[1]) < |
| 63 | std::tie(other.words_[0], other.words_[1]); |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Generates a string representation of this Token useful for e.g. logging. |
| 67 | std::string ToString() const; |
| 68 | |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 69 | // FromString is the opposite of ToString. It returns absl::nullopt if the |
| 70 | // |string_representation| is invalid. |
| 71 | static absl::optional<Token> FromString(StringPiece string_representation); |
| 72 | |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 73 | private: |
| 74 | // Note: Two uint64_t are used instead of uint8_t[16] in order to have a |
| 75 | // simpler implementation, paricularly for |ToString()|, |is_zero()|, and |
| 76 | // constexpr value construction. |
Liza Burakova | e7e94301 | 2021-07-16 14:09:05 | [diff] [blame] | 77 | |
| 78 | uint64_t words_[2] = {0, 0}; |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // For use in std::unordered_map. |
| 82 | struct TokenHash { |
| 83 | size_t operator()(const base::Token& token) const { |
| 84 | return base::HashInts64(token.high(), token.low()); |
| 85 | } |
| 86 | }; |
| 87 | |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 88 | class Pickle; |
| 89 | class PickleIterator; |
| 90 | |
| 91 | // For serializing and deserializing Token values. |
| 92 | BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token); |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 93 | BASE_EXPORT absl::optional<Token> ReadTokenFromPickle( |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 94 | PickleIterator* pickle_iterator); |
| 95 | |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 96 | } // namespace base |
| 97 | |
| 98 | #endif // BASE_TOKEN_H_ |