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.
5 #include "base/unguessable_token.h"
7 #include "base/format_macros.h"
8 #include "base/no_destructor.h"
9 #include "base/rand_util.h"
10 #include "base/strings/stringprintf.h"
14 UnguessableToken::UnguessableToken(const base::Token& token) : token_(token) {}
17 UnguessableToken UnguessableToken::Create() {
18 return UnguessableToken(Token::CreateRandom());
22 const UnguessableToken& UnguessableToken::Null() {
23 static const NoDestructor<UnguessableToken> null_token;
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});
35 std::ostream& operator<<(std::ostream& out, const UnguessableToken& token) {
36 return out << "(" << token.ToString() << ")";