Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Side by Side Diff: base/unguessable_token.cc

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Serialize --> Serialization getters Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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("(%08" PRIX64 "%08" PRIX64 ")", high_, low_);
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 uint64_t UnguessableToken::GetHighForSerialization() const {
30 // Serializing an empty UnguessableToken is a security issue.
dcheng 2016/09/17 00:22:20 Just inline this. logging.h is already in our head
tguilbert 2016/09/17 00:49:49 Done.
31 DCHECK(!is_empty());
32 return high_;
33 }
34
35 uint64_t UnguessableToken::GetLowForSerialization() const {
36 // Serializing an empty UnguessableToken is a security issue.
37 DCHECK(!is_empty());
38 return low_;
39 }
40
41 // static
42 UnguessableToken UnguessableToken::Deserialize(uint64_t high, uint64_t low) {
43 // Receiving a zeroed out UnguessableToken from another process means that it
44 // was never initialized via Create(). Treat this case as a security issue.
45 DCHECK(!(high == 0 && low == 0));
46 return UnguessableToken(high, low);
47 }
48
49 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698