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

Side by Side Diff: base/unguessable_token.h

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Removed MOJO_COMMON_EXPORT 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
« no previous file with comments | « base/BUILD.gn ('k') | base/unguessable_token.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef BASE_UNGUESSABLE_TOKEN_H_
6 #define BASE_UNGUESSABLE_TOKEN_H_
7
8 #include <stdint.h>
9 #include <string.h>
10 #include <tuple>
11
12 #include "base/base_export.h"
13 #include "base/hash.h"
14 #include "base/logging.h"
15
16 namespace base {
17
18 struct UnguessableTokenHash;
19
20 // A UnguessableToken is an 128-bit token generated from a cryptographically
21 // strong random source.
22 //
23 // UnguessableToken should be used when a sensitive ID needs to be unguessable,
24 // and is shared across processes. It can be used as part of a larger aggregate
25 // type, or as an ID in and of itself.
26 //
27 // Use Create() for creating new UnguessableTokens.
28 //
29 // NOTE: It is illegal to send empty UnguessableTokens across processes, and
30 // sending/receiving empty tokens should be treated as a security issue.
31 // If there is a valid scenario for sending "no token" across processes,
32 // base::Optional should be used instead of an empty token.
33 class BASE_EXPORT UnguessableToken {
34 public:
35 // Create a unique UnguessableToken.
36 static UnguessableToken Create();
37
38 // Return a UnguessableToken built from the high/low bytes provided.
39 // It should only be used in deserialization scenarios.
40 //
41 // NOTE: If the deserialized token is empty, it means that it was never
42 // initialized via Create(). This is a security issue, and should be handled.
43 static UnguessableToken Deserialize(uint64_t high, uint64_t low);
44
45 // Creates an empty UnguessableToken.
46 // Assign to it with Create() before using it.
47 UnguessableToken() = default;
48
49 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
50 uint64_t GetHighForSerialization() const {
51 DCHECK(!is_empty());
52 return high_;
53 };
54
55 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
56 uint64_t GetLowForSerialization() const {
57 DCHECK(!is_empty());
58 return low_;
59 }
60
61 bool is_empty() const { return high_ == 0 && low_ == 0; }
62
63 std::string ToString() const;
64
65 explicit operator bool() const { return !is_empty(); }
66
67 bool operator<(const UnguessableToken& other) const {
68 return std::tie(high_, low_) < std::tie(other.high_, other.low_);
69 }
70
71 bool operator==(const UnguessableToken& other) const {
72 return high_ == other.high_ && low_ == other.low_;
73 }
74
75 bool operator!=(const UnguessableToken& other) const {
76 return !(*this == other);
77 }
78
79 private:
80 friend struct UnguessableTokenHash;
81 UnguessableToken(uint64_t high, uint64_t low);
82
83 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a
84 // simpler ToString() and is_empty().
85 uint64_t high_ = 0;
86 uint64_t low_ = 0;
87 };
88
89 // For use in std::unordered_map.
90 struct UnguessableTokenHash {
91 size_t operator()(const base::UnguessableToken& token) const {
92 DCHECK(token);
93 return base::HashInts64(token.high_, token.low_);
94 }
95 };
96
97 } // namespace base
98
99 #endif // BASE_UNGUESSABLE_TOKEN_H_
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/unguessable_token.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698