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 | #include "base/token.h" |
| 6 | |
| 7 | #include <inttypes.h> |
| 8 | |
Jan Keitel | 601e2763 | 2024-07-28 09:56:41 | [diff] [blame] | 9 | #include <array> |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 10 | #include <optional> |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 11 | #include <string_view> |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 12 | |
Maksim Ivanov | 6177f0e | 2022-12-14 00:32:49 | [diff] [blame] | 13 | #include "base/check.h" |
Lei Zhang | f3c81cb | 2023-09-29 18:07:17 | [diff] [blame] | 14 | #include "base/hash/hash.h" |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 15 | #include "base/pickle.h" |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 16 | #include "base/rand_util.h" |
| 17 | #include "base/strings/stringprintf.h" |
| 18 | |
| 19 | namespace base { |
| 20 | |
| 21 | // static |
| 22 | Token Token::CreateRandom() { |
| 23 | Token token; |
| 24 | |
| 25 | // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the |
| 26 | // base version directly, and to prevent the dependency from base/ to crypto/. |
danakj | 95305d27 | 2024-05-09 20:38:44 | [diff] [blame] | 27 | RandBytes(byte_span_from_ref(token)); |
Maksim Ivanov | 6177f0e | 2022-12-14 00:32:49 | [diff] [blame] | 28 | |
| 29 | CHECK(!token.is_zero()); |
| 30 | |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 31 | return token; |
| 32 | } |
| 33 | |
| 34 | std::string Token::ToString() const { |
danakj | 95305d27 | 2024-05-09 20:38:44 | [diff] [blame] | 35 | return StringPrintf("%016" PRIX64 "%016" PRIX64, words_[0], words_[1]); |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 36 | } |
| 37 | |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 38 | // static |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 39 | std::optional<Token> Token::FromString(std::string_view string_representation) { |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 40 | if (string_representation.size() != 32) { |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 41 | return std::nullopt; |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 42 | } |
Jan Keitel | 601e2763 | 2024-07-28 09:56:41 | [diff] [blame] | 43 | std::array<uint64_t, 2> words; |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 44 | for (size_t i = 0; i < 2; i++) { |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 45 | uint64_t word = 0; |
| 46 | // This j loop is similar to HexStringToUInt64 but we are intentionally |
| 47 | // strict about case, accepting 'A' but rejecting 'a'. |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 48 | for (size_t j = 0; j < 16; j++) { |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 49 | const char c = string_representation[(16 * i) + j]; |
| 50 | if (('0' <= c) && (c <= '9')) { |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 51 | word = (word << 4) | static_cast<uint64_t>(c - '0'); |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 52 | } else if (('A' <= c) && (c <= 'F')) { |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 53 | word = (word << 4) | static_cast<uint64_t>(c - 'A' + 10); |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 54 | } else { |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 55 | return std::nullopt; |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | words[i] = word; |
| 59 | } |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 60 | return std::optional<Token>(std::in_place, words[0], words[1]); |
Nigel Tao | 9af13b8 | 2022-05-19 02:01:16 | [diff] [blame] | 61 | } |
| 62 | |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 63 | void WriteTokenToPickle(Pickle* pickle, const Token& token) { |
| 64 | pickle->WriteUInt64(token.high()); |
| 65 | pickle->WriteUInt64(token.low()); |
| 66 | } |
| 67 | |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 68 | std::optional<Token> ReadTokenFromPickle(PickleIterator* pickle_iterator) { |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 69 | uint64_t high; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 70 | if (!pickle_iterator->ReadUInt64(&high)) { |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 71 | return std::nullopt; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 72 | } |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 73 | |
| 74 | uint64_t low; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 75 | if (!pickle_iterator->ReadUInt64(&low)) { |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 76 | return std::nullopt; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 77 | } |
Collin Baker | ffe1d0eb | 2019-08-05 23:00:45 | [diff] [blame] | 78 | |
| 79 | return Token(high, low); |
| 80 | } |
| 81 | |
Lei Zhang | f3c81cb | 2023-09-29 18:07:17 | [diff] [blame] | 82 | size_t TokenHash::operator()(const Token& token) const { |
| 83 | return HashInts64(token.high(), token.low()); |
| 84 | } |
| 85 | |
Ken Rockot | 8c6991c7 | 2018-11-07 21:23:19 | [diff] [blame] | 86 | } // namespace base |