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 #include "base/unguessable_token.h" | |
6 | |
7 #include "base/format_macros.h" | |
8 #include "base/rand_util.h" | |
9 #include "base/strings/stringprintf.h" | |
10 | |
11 namespace base { | |
12 | |
13 UnguessableToken::UnguessableToken(uint64_t high, uint64_t low) | |
14 : high_(high), low_(low) {} | |
15 | |
16 std::string UnguessableToken::ToString() const { | |
17 return base::StringPrintf("(%" PRIu64 ":%" PRIu64 ")", high_, low_); | |
sandersd (OOO until July 31)
2016/09/16 22:13:55
I would suggest 0-padded hex rather than exposing
tguilbert
2016/09/16 22:48:14
Done and added UT.
| |
18 } | |
19 | |
20 // static | |
21 UnguessableToken UnguessableToken::Create() { | |
22 UnguessableToken token; | |
23 // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the | |
24 // base version directly, and to prevent the dependency from base/ to crypto/. | |
25 base::RandBytes(&token, sizeof(token)); | |
26 return token; | |
27 } | |
28 | |
29 void UnguessableToken::Serialize(uint64_t* high_out, uint64_t* low_out) const { | |
30 // Serializing an uninitialized UnguessableToken is a security issue. | |
31 // Use base::Optional if there is a valid use for sending "no token". | |
32 DCHECK(!is_empty()); | |
33 *high_out = high_; | |
34 *low_out = low_; | |
35 } | |
36 | |
37 // static | |
38 UnguessableToken UnguessableToken::Deserialize(uint64_t high, uint64_t low) { | |
39 // Receiving a zeroed out UnguessableToken from another process means that it | |
40 // was never initialized via Create(). Treat this case as a security issue. | |
41 DCHECK(!(high == 0 && low == 0)); | |
42 return UnguessableToken(high, low); | |
43 } | |
44 | |
45 } // namespace base | |
OLD | NEW |