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

Side by Side Diff: base/nonce.h

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Removed '= default' constructor. 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/nonce.cc » ('j') | base/nonce.cc » ('J')
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_NONCE_H_
6 #define BASE_NONCE_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
15 namespace base {
16
17 // An Nonce is a cryptographically strong, randomly generated, unguessable 128
dcheng 2016/09/13 23:19:05 A Nonce is an unguessable 128-bit token generated
tguilbert 2016/09/14 22:40:47 Done. However, I don't know what all useful scenar
18 // bit token.
19 //
20 // TO CREATE NEW NONCES, ONLY USE GENERATE()!
dcheng 2016/09/13 23:19:05 Suggested wording: Use Generate() for creating ne
tguilbert 2016/09/14 22:40:47 TY for the better wording! Done.
21 // The Generate() function is guaranteed to be cryptographically strong random.
22 // Deserialize() is only meant to deserialize Nonces that were created via
23 // Generate(), and sent across processes as two uint64_t.
24 class BASE_EXPORT Nonce {
25 public:
26 // Generates a unique, strongly random, unguessable nonce.
dcheng 2016/09/13 23:19:05 Ditto: "Create an unguessable and unique nonce."
tguilbert 2016/09/14 22:40:47 Done.
27 // USE ONLY THIS FUNCTION TO GENERATE NEW NONCES.
28 static Nonce Generate();
Ken Rockot(use gerrit already) 2016/09/13 23:21:13 Why Generate? Why can't the default constructor ju
sandersd (OOO until July 31) 2016/09/13 23:51:53 I do like the clarity of explicitly calling Genera
tguilbert 2016/09/14 22:40:47 As per the discussions floating around, I have mai
29
30 // Returns a nonce built from the high/low bytes provided.
31 // It should only be used in deserialization scenarios.
32 static Nonce Deserialize(uint64_t high, uint64_t low);
33
34 // Creates a 0 initiliazed Nonce.
dcheng 2016/09/13 23:19:05 Nit: Create a zero-initialized Nonce (so it doesn'
Ken Rockot(use gerrit already) 2016/09/13 23:21:13 nit: initialized (typo)
tguilbert 2016/09/14 22:40:47 Done.
35 Nonce();
36
37 bool is_empty() const { return high_ == 0 && low_ == 0; }
38
39 uint64_t high() const { return high_; }
40
41 uint64_t low() const { return low_; }
42
43 std::string ToString() const;
44
45 bool operator<(const Nonce& other) const {
46 return std::tie(high_, low_) < std::tie(other.high_, other.low_);
47 }
48
49 bool operator==(const Nonce& other) const {
50 return high_ == other.high_ && low_ == other.low_;
51 }
52
53 bool operator!=(const Nonce& other) const { return !operator==(other); }
54
55 private:
56 Nonce(uint64_t high, uint64_t low);
57
58 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a
59 // simpler ToString() and is_null().
60 uint64_t high_;
61 uint64_t low_;
62 };
63
64 // For use in std::unordered_map.
65 struct NonceHash {
66 size_t operator()(const base::Nonce& nonce) const {
67 return base::HashInts64(nonce.high(), nonce.low());
68 }
69 };
70
71 // If base::Nonce is no longer 128 bits, the IPC serialization logic and Mojo
DaleCurtis 2016/09/13 23:39:48 Drive-by: Worth moving to C++ file to avoid all in
tguilbert 2016/09/14 22:40:47 Done.
72 // StructTraits should be updated to match the size of the struct.
73 static_assert(sizeof(Nonce) == 2 * sizeof(uint64_t),
74 "base::Nonce should be of size 2 * sizeof(uint64_t).");
75
76 } // namespace base
77
78 #endif // BASE_NONCE_H_
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/nonce.cc » ('j') | base/nonce.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698