Chromium Code Reviews| 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 #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 | |
| 18 // bit token. | |
| 19 // | |
| 20 // TO CREATE NEW NONCES, ONLY USE GENERATE()! | |
| 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 accross processes as two uint64_t. | |
| 24 class BASE_EXPORT Nonce { | |
| 25 public: | |
| 26 // Generates a unique, strongly random, unguessable nonce. | |
| 27 // USE THIS FUNCTION TO GENERATE NEW NONCES. | |
| 28 static Nonce Generate(); | |
| 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 null Nonce. | |
|
Tom Sepez
2016/09/13 18:20:41
Actually, wouldn't this create an uninitialized no
tguilbert
2016/09/13 20:20:19
It does create a (0,0) Nonce (and I have a UT to m
Tom Sepez
2016/09/13 21:29:39
Ok, yes if the ctor is called, so your test Nonce(
| |
| 35 Nonce() = default; | |
| 36 | |
| 37 bool is_null() 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::unorderer_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 not a POD, the IPC serialization logic should be revisited, | |
| 72 // since it might no longer be possible to memcpy base::Nonce safely. | |
| 73 static_assert(std::is_pod<Nonce>::value, "base::Nonce should be of POD type."); | |
| 74 | |
| 75 } // namespace base | |
| 76 | |
| 77 #endif // BASE_NONCE_H_ | |
| OLD | NEW |