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 #include "base/unguessable_token.h" 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 UnguessableToken(const base::Token & token)14UnguessableToken::UnguessableToken(const base::Token& token) : token_(token) {} 15 16 // static Create()17UnguessableToken UnguessableToken::Create() { 18 return UnguessableToken(Token::CreateRandom()); 19 } 20 21 // static Null()22const UnguessableToken& UnguessableToken::Null() { 23 static const NoDestructor<UnguessableToken> null_token; 24 return *null_token; 25 } 26 27 // static Deserialize(uint64_t high,uint64_t low)28UnguessableToken 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 operator <<(std::ostream & out,const UnguessableToken & token)35std::ostream& operator<<(std::ostream& out, const UnguessableToken& token) { 36 return out << "(" << token.ToString() << ")"; 37 } 38 39 } // namespace base 40