| 6 |
| 7 | #include "base/format_macros.h" |
| 8 | #include "base/no_destructor.h" |
| 9 | #include "base/rand_util.h" |
| 10 | #include "base/strings/stringprintf.h" |
| 11 |
| 12 | namespace base { |
| 13 |
| 14 | UnguessableToken::UnguessableToken(const base::Token& token) : token_(token) {} |
| 15 |
| 16 | // static |
| 17 | UnguessableToken UnguessableToken::Create() { |
| 18 | return UnguessableToken(Token::CreateRandom()); |
| 19 | } |
| 20 |
| 21 | // static |
| 22 | const UnguessableToken& UnguessableToken::Null() { |
| 23 | static const NoDestructor<UnguessableToken> null_token; |
| 24 | return *null_token; |
| 25 | } |
| 26 |
| 27 | // static |
| 28 | UnguessableToken UnguessableToken::Deserialize(uint64_t high, uint64_t low) { |
| 29 | // Receiving a zeroed out UnguessableToken from another process means that it |
| 30 | // was never initialized via Create(). Treat this case as a security issue. |
| 31 | DCHECK(!(high == 0 && low == 0)); |
| 32 | return UnguessableToken(Token{high, low}); |
| 33 | } |
| 34 |
| 35 | std::ostream& operator<<(std::ostream& out, const UnguessableToken& token) { |
| 36 | return out << "(" << token.ToString() << ")"; |
| 37 | } |
| 38 |
| 39 | } // namespace base |
| 40 |