OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 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_UNGUESSABLE_TOKEN_H_ |
| 6 #define BASE_UNGUESSABLE_TOKEN_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <string.h> |
| 10 #include <tuple> |
| 11 |
| 12 #include "base/base_export.h" |
| 13 #include "base/hash.h" |
| 14 #include "base/logging.h" |
| 15 |
| 16 namespace base { |
| 17 |
| 18 struct UnguessableTokenHash; |
| 19 |
| 20 // A UnguessableToken is an 128-bit token generated from a cryptographically |
| 21 // strong random source. |
| 22 // |
| 23 // UnguessableToken should be used when a sensitive ID needs to be unguessable, |
| 24 // and is shared across processes. It can be used as part of a larger aggregate |
| 25 // type, or as an ID in and of itself. |
| 26 // |
| 27 // Use Create() for creating new UnguessableTokens. |
| 28 // |
| 29 // NOTE: It is illegal to send empty UnguessableTokens across processes, and |
| 30 // sending/receiving empty tokens should be treated as a security issue. |
| 31 // If there is a valid scenario for sending "no token" across processes, |
| 32 // base::Optional should be used instead of an empty token. |
| 33 class BASE_EXPORT UnguessableToken { |
| 34 public: |
| 35 // Create a unique UnguessableToken. |
| 36 static UnguessableToken Create(); |
| 37 |
| 38 // Return a UnguessableToken built from the high/low bytes provided. |
| 39 // It should only be used in deserialization scenarios. |
| 40 // |
| 41 // NOTE: If the deserialized token is empty, it means that it was never |
| 42 // initialized via Create(). This is a security issue, and should be handled. |
| 43 static UnguessableToken Deserialize(uint64_t high, uint64_t low); |
| 44 |
| 45 // Creates an empty UnguessableToken. |
| 46 // Assign to it with Create() before using it. |
| 47 UnguessableToken() = default; |
| 48 |
| 49 // Return |high_| or |low_| for serialization. |
| 50 // NOTE: Serializing an empty UnguessableToken is an illegal operation. |
| 51 // Use base::Optional if there is a valid use for sending "no token". |
| 52 uint64_t GetHighForSerialization() const; |
| 53 uint64_t GetLowForSerialization() const; |
| 54 |
| 55 bool is_empty() const { return high_ == 0 && low_ == 0; } |
| 56 |
| 57 std::string ToString() const; |
| 58 |
| 59 explicit operator bool() const { return !is_empty(); } |
| 60 |
| 61 bool operator<(const UnguessableToken& other) const { |
| 62 return std::tie(high_, low_) < std::tie(other.high_, other.low_); |
| 63 } |
| 64 |
| 65 bool operator==(const UnguessableToken& other) const { |
| 66 return high_ == other.high_ && low_ == other.low_; |
| 67 } |
| 68 |
| 69 bool operator!=(const UnguessableToken& other) const { |
| 70 return !(*this == other); |
| 71 } |
| 72 |
| 73 private: |
| 74 friend struct UnguessableTokenHash; |
| 75 UnguessableToken(uint64_t high, uint64_t low); |
| 76 |
| 77 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a |
| 78 // simpler ToString() and is_empty(). |
| 79 uint64_t high_ = 0; |
| 80 uint64_t low_ = 0; |
| 81 }; |
| 82 |
| 83 // For use in std::unordered_map. |
| 84 struct UnguessableTokenHash { |
| 85 size_t operator()(const base::UnguessableToken& token) const { |
| 86 DCHECK(token); |
| 87 return base::HashInts64(token.high_, token.low_); |
| 88 } |
| 89 }; |
| 90 |
| 91 } // namespace base |
| 92 |
| 93 #endif // BASE_UNGUESSABLE_TOKEN_H_ |
OLD | NEW |