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

Unified Diff: base/nonce_token.cc

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Adressed comments 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 side-by-side diff with in-line comments
Download patch
Index: base/nonce_token.cc
diff --git a/base/nonce_token.cc b/base/nonce_token.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0863e3e9583971d81aa9e429189aa7962ae8f30b
--- /dev/null
+++ b/base/nonce_token.cc
@@ -0,0 +1,43 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/nonce_token.h"
+
+#include "base/format_macros.h"
+#include "base/rand_util.h"
+#include "base/strings/stringprintf.h"
+
+namespace base {
+
+NonceToken::NonceToken(uint64_t high, uint64_t low) : high_(high), low_(low) {}
+
+std::string NonceToken::ToString() const {
+ return base::StringPrintf("(%" PRIu64 ":%" PRIu64 ")", high_, low_);
+}
+
+// static
+NonceToken NonceToken::Create() {
+ NonceToken token;
+ // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
+ // base version directly, and to prevent the dependency from base/ to crypto/.
+ base::RandBytes(&token, sizeof(token));
+ return token;
+}
+
+void NonceToken::Serialize(uint64_t* high_out, uint64_t* low_out) const {
+ // Serializing an uninitialized NonceToken is a security issue.
+ CHECK(!is_empty());
+ *high_out = high_;
+ *low_out = low_;
+}
+
+// static
+NonceToken NonceToken::Deserialize(uint64_t high, uint64_t low) {
+ // Sending a zeroed out NonceToken across processes means that it was never
+ // initialized via NonceToken::Create(), which is a security issue.
+ CHECK(!NonceToken::IsZeroData(high, low));
+ return NonceToken(high, low);
+}
+
+} // namespace base
« base/nonce_token.h ('K') | « base/nonce_token.h ('k') | base/nonce_token_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698