blob: cc47f0356f195c045552a454f4523a48cb83008c [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
Jan Keitel601e27632024-07-28 09:56:4110#include <array>
Joe Mason0c06b4b2023-11-28 00:17:0411#include <compare>
Arthur Sonzognie5fff99c2024-02-21 15:58:2412#include <optional>
Victor Costan57f635e2020-07-08 04:08:2213#include <string>
Helmut Januschka1dce9dc2024-06-11 13:05:3514#include <string_view>
Daniel Chengc57cd972025-04-03 18:18:4815#include <utility>
Ken Rockot8c6991c72018-11-07 21:23:1916
17#include "base/base_export.h"
Liza Burakovae7e943012021-07-16 14:09:0518#include "base/containers/span.h"
Ken Rockot8c6991c72018-11-07 21:23:1919
20namespace base {
21
22// A Token is a randomly chosen 128-bit integer. This class supports generation
23// from a cryptographically strong random source, or constexpr construction over
24// fixed values (e.g. to store a pre-generated constant value). Tokens are
25// similar in spirit and purpose to UUIDs, without many of the constraints and
26// expectations (such as byte layout and string representation) clasically
27// associated with UUIDs.
28class BASE_EXPORT Token {
29 public:
30 // Constructs a zero Token.
Victor Costan57f635e2020-07-08 04:08:2231 constexpr Token() = default;
Ken Rockot8c6991c72018-11-07 21:23:1932
33 // Constructs a Token with |high| and |low| as its contents.
Liza Burakovae7e943012021-07-16 14:09:0534 constexpr Token(uint64_t high, uint64_t low) : words_{high, low} {}
Ken Rockot8c6991c72018-11-07 21:23:1935
Victor Costan57f635e2020-07-08 04:08:2236 constexpr Token(const Token&) = default;
37 constexpr Token& operator=(const Token&) = default;
38 constexpr Token(Token&&) noexcept = default;
39 constexpr Token& operator=(Token&&) = default;
40
Ken Rockot8c6991c72018-11-07 21:23:1941 // Constructs a new Token with random |high| and |low| values taken from a
Maksim Ivanov6177f0e2022-12-14 00:32:4942 // cryptographically strong random source. The result's |is_zero()| is
43 // guaranteed to be false.
Ken Rockot8c6991c72018-11-07 21:23:1944 static Token CreateRandom();
45
46 // The high and low 64 bits of this Token.
Liza Burakovae7e943012021-07-16 14:09:0547 constexpr uint64_t high() const { return words_[0]; }
48 constexpr uint64_t low() const { return words_[1]; }
Ken Rockot8c6991c72018-11-07 21:23:1949
Liza Burakovae7e943012021-07-16 14:09:0550 constexpr bool is_zero() const { return words_[0] == 0 && words_[1] == 0; }
51
Peter Kastingae7e314f2024-11-27 18:04:0752 span<const uint8_t, 16> AsBytes() const { return as_byte_span(words_); }
Ken Rockot8c6991c72018-11-07 21:23:1953
Joe Mason0c06b4b2023-11-28 00:17:0454 friend constexpr auto operator<=>(const Token& lhs,
55 const Token& rhs) = default;
56 friend constexpr bool operator==(const Token& lhs,
57 const Token& rhs) = default;
Ken Rockot8c6991c72018-11-07 21:23:1958
Daniel Chengc57cd972025-04-03 18:18:4859 template <typename H>
60 friend H AbslHashValue(H h, const Token& token) {
61 return H::combine(std::move(h), token.words_);
62 }
63
Ken Rockot8c6991c72018-11-07 21:23:1964 // Generates a string representation of this Token useful for e.g. logging.
65 std::string ToString() const;
66
Arthur Sonzognie5fff99c2024-02-21 15:58:2467 // FromString is the opposite of ToString. It returns std::nullopt if the
Nigel Tao9af13b82022-05-19 02:01:1668 // |string_representation| is invalid.
Helmut Januschka1dce9dc2024-06-11 13:05:3569 static std::optional<Token> FromString(
70 std::string_view string_representation);
Nigel Tao9af13b82022-05-19 02:01:1671
Ken Rockot8c6991c72018-11-07 21:23:1972 private:
73 // Note: Two uint64_t are used instead of uint8_t[16] in order to have a
Jan Keitel601e27632024-07-28 09:56:4174 // simpler implementation, particularly for |ToString()|, |is_zero()|, and
Ken Rockot8c6991c72018-11-07 21:23:1975 // constexpr value construction.
Liza Burakovae7e943012021-07-16 14:09:0576
Jan Keitel601e27632024-07-28 09:56:4177 std::array<uint64_t, 2> words_ = {0, 0};
Ken Rockot8c6991c72018-11-07 21:23:1978};
79
80// For use in std::unordered_map.
Lei Zhangf3c81cb2023-09-29 18:07:1781struct BASE_EXPORT TokenHash {
82 size_t operator()(const Token& token) const;
Ken Rockot8c6991c72018-11-07 21:23:1983};
84
Collin Bakerffe1d0eb2019-08-05 23:00:4585class Pickle;
86class PickleIterator;
87
88// For serializing and deserializing Token values.
89BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token);
Arthur Sonzognie5fff99c2024-02-21 15:58:2490BASE_EXPORT std::optional<Token> ReadTokenFromPickle(
Collin Bakerffe1d0eb2019-08-05 23:00:4591 PickleIterator* pickle_iterator);
92
Ken Rockot8c6991c72018-11-07 21:23:1993} // namespace base
94
95#endif // BASE_TOKEN_H_