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 #ifndef BASE_NONCE_H_ | |
6 #define BASE_NONCE_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <string.h> | |
10 | |
11 #include "base/base_export.h" | |
12 #include "base/format_macros.h" | |
13 #include "base/hash.h" | |
14 #include "base/strings/stringprintf.h" | |
15 | |
16 namespace base { | |
17 | |
18 // An Nonce is a cryptographically strong, randomly generated, unguessable 128 | |
19 // bit token. | |
20 // | |
21 // PLEASE MAKE SURE YOUR USAGE OF THIS CLASS IS SECURE! | |
danakj
2016/09/12 20:41:57
You should explain how to make sure or this commen
tguilbert
2016/09/12 23:53:33
Updated.
| |
22 // | |
23 struct BASE_EXPORT Nonce { | |
24 Nonce() : high(0), low(0){}; | |
danakj
2016/09/12 20:41:57
= default;
move the defn to a .cc file
tguilbert
2016/09/12 23:53:33
I have updated it to be "= default". If you have n
| |
25 Nonce(uint64_t high, uint64_t low) : high(high), low(low){}; | |
danakj
2016/09/12 20:41:57
dittos
tguilbert
2016/09/12 23:53:33
Done.
| |
26 | |
27 // Generates a unique, strongly random, unguessable nonce. | |
28 static Nonce Generate(); | |
danakj
2016/09/12 20:41:57
maybe this should go first in the class so people
Fady Samuel
2016/09/12 20:47:47
Do we want to expose manually setting the nonce to
tguilbert
2016/09/12 23:53:33
I agree with you and have changed the class to be
| |
29 | |
30 bool is_null() const { return high == 0 && low == 0; }; | |
danakj
2016/09/12 20:41:57
no ; after a function body
tguilbert
2016/09/12 23:53:34
Done.
| |
31 | |
32 std::string ToString() const { | |
33 return base::StringPrintf("(%" PRIu64 ":%" PRIu64 ")", high, low); | |
danakj
2016/09/12 20:41:57
move this fn defn to a .cc file
tguilbert
2016/09/12 23:53:33
Done.
| |
34 } | |
35 | |
36 // Acceptable hash for use in a std::unordered_map, | |
37 // but not cryptographically secure. | |
38 size_t hash() const { return base::HashInts64(high, low); } | |
danakj
2016/09/12 20:41:57
Think this should be part of the hash struct, rath
tguilbert
2016/09/12 23:53:34
Done.
| |
39 | |
40 bool operator<(const Nonce& other) const { | |
41 return high == other.high ? low < other.low : high < other.high; | |
danakj
2016/09/12 20:41:57
use std::tie
tguilbert
2016/09/12 23:53:33
Done.
| |
42 } | |
43 | |
44 bool operator==(const Nonce& other) const { | |
45 return high == other.high && low == other.low; | |
46 } | |
47 | |
48 bool operator!=(const Nonce& other) const { return !operator==(other); } | |
49 | |
50 // Note: two uint64_t are used instead of uint8_t[16], in order to have a | |
danakj
2016/09/12 20:41:57
Two
tguilbert
2016/09/12 23:53:33
Done.
| |
51 // simpler ToString(), is_null() and hash() functions. | |
52 uint64_t high; | |
Fady Samuel
2016/09/12 20:47:47
I'm a bit worried about exposing these because tha
tguilbert
2016/09/12 23:53:33
I agree with you. I wasn't sure in which ways one
| |
53 uint64_t low; | |
54 }; | |
55 | |
56 } // namespace base | |
57 | |
58 namespace std { | |
59 // Specialize default hash for use in std::unordered_map. | |
danakj
2016/09/12 20:41:57
You can provider a struct to do the hash if you li
tguilbert
2016/09/12 23:53:33
Done. TY!
| |
60 template <> | |
61 struct hash<base::Nonce> { | |
62 size_t operator()(const base::Nonce& nonce) const { return nonce.hash(); } | |
63 }; | |
64 } | |
65 | |
66 #endif // BASE_NONCE_H_ | |
OLD | NEW |