blob: 4ad0ebf01f7f08d35a336f733aab5058546277aa [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Ken Rockot8c6991c72018-11-07 21:23:192// 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 Costan57f635e2020-07-08 04:08:2210#include <string>
Ken Rockot8c6991c72018-11-07 21:23:1911#include <tuple>
12
13#include "base/base_export.h"
Liza Burakovae7e943012021-07-16 14:09:0514#include "base/containers/span.h"
Daniel Chengc0581992019-03-29 04:52:5615#include "base/hash/hash.h"
Anton Bikineev7dd58ad2021-05-18 01:01:3916#include "third_party/abseil-cpp/absl/types/optional.h"
Ken Rockot8c6991c72018-11-07 21:23:1917
18namespace 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.
26class BASE_EXPORT Token {
27 public:
28 // Constructs a zero Token.
Victor Costan57f635e2020-07-08 04:08:2229 constexpr Token() = default;
Ken Rockot8c6991c72018-11-07 21:23:1930
31 // Constructs a Token with |high| and |low| as its contents.
Liza Burakovae7e943012021-07-16 14:09:0532 constexpr Token(uint64_t high, uint64_t low) : words_{high, low} {}
Ken Rockot8c6991c72018-11-07 21:23:1933
Victor Costan57f635e2020-07-08 04:08:2234 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 Rockot8c6991c72018-11-07 21:23:1939 // 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 Burakovae7e943012021-07-16 14:09:0544 constexpr uint64_t high() const { return words_[0]; }
45 constexpr uint64_t low() const { return words_[1]; }
Ken Rockot8c6991c72018-11-07 21:23:1946
Liza Burakovae7e943012021-07-16 14:09:0547 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 Rockot8c6991c72018-11-07 21:23:1952
Victor Costan57f635e2020-07-08 04:08:2253 constexpr bool operator==(const Token& other) const {
Liza Burakovae7e943012021-07-16 14:09:0554 return words_[0] == other.words_[0] && words_[1] == other.words_[1];
Ken Rockot8c6991c72018-11-07 21:23:1955 }
56
Victor Costan57f635e2020-07-08 04:08:2257 constexpr bool operator!=(const Token& other) const {
58 return !(*this == other);
59 }
Ken Rockot8c6991c72018-11-07 21:23:1960
Victor Costan57f635e2020-07-08 04:08:2261 constexpr bool operator<(const Token& other) const {
Liza Burakovae7e943012021-07-16 14:09:0562 return std::tie(words_[0], words_[1]) <
63 std::tie(other.words_[0], other.words_[1]);
Ken Rockot8c6991c72018-11-07 21:23:1964 }
65
66 // Generates a string representation of this Token useful for e.g. logging.
67 std::string ToString() const;
68
Nigel Tao9af13b82022-05-19 02:01:1669 // 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 Rockot8c6991c72018-11-07 21:23:1973 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 Burakovae7e943012021-07-16 14:09:0577
78 uint64_t words_[2] = {0, 0};
Ken Rockot8c6991c72018-11-07 21:23:1979};
80
81// For use in std::unordered_map.
82struct TokenHash {
83 size_t operator()(const base::Token& token) const {
84 return base::HashInts64(token.high(), token.low());
85 }
86};
87
Collin Bakerffe1d0eb2019-08-05 23:00:4588class Pickle;
89class PickleIterator;
90
91// For serializing and deserializing Token values.
92BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token);
Anton Bikineev7dd58ad2021-05-18 01:01:3993BASE_EXPORT absl::optional<Token> ReadTokenFromPickle(
Collin Bakerffe1d0eb2019-08-05 23:00:4594 PickleIterator* pickle_iterator);
95
Ken Rockot8c6991c72018-11-07 21:23:1996} // namespace base
97
98#endif // BASE_TOKEN_H_