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

Unified Diff: crypto/random.cc

Issue 6873156: Move crypto_helpers from sync to base (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Move crypto_helpers to crypto instead of base. Created 9 years, 8 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: crypto/random.cc
diff --git a/crypto/random.cc b/crypto/random.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f06aac393d3bb300e567c3616ce551ffedabe482
--- /dev/null
+++ b/crypto/random.cc
@@ -0,0 +1,30 @@
+// Copyright (c) 2011 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 "crypto/random.h"
+
+#include <string>
+
+#include "base/base64.h"
+#include "base/rand_util.h"
+
+void GetRandomBytes(char* output, int output_length) {
+ uint64 random_int;
+ const char* random_int_bytes = reinterpret_cast<const char*>(&random_int);
+ int random_int_size = sizeof(random_int);
+ for (int i = 0; i < output_length; i += random_int_size) {
+ random_int = base::RandUint64();
agl 2011/04/26 13:59:37 This is not cryptographically strong on Windows.
+ int copy_count = std::min(output_length - i, random_int_size);
+ memcpy(output + i, random_int_bytes, copy_count);
+ }
+}
+
+std::string Generate128BitRandomHexString() {
+ const int kNumberBytes = 128 / 8;
+ std::string random_bytes(kNumberBytes, ' ');
agl 2011/04/26 13:59:37 no need for a std::string here, a uint8[] will do.
qsr (NOT THE RIGHT qsr) 2011/04/26 15:24:18 Except that the base64_encode function expect stri
+ GetRandomBytes(&random_bytes[0], kNumberBytes);
+ std::string base64_encoded_bytes;
+ base::Base64Encode(random_bytes, &base64_encoded_bytes);
agl 2011/04/26 13:59:37 The function name suggests that the data should be
qsr (NOT THE RIGHT qsr) 2011/04/26 15:24:18 As this is a refactoring, I will modify the name o
+ return base64_encoded_bytes;
+}
« crypto/random.h ('K') | « crypto/random.h ('k') | crypto/random_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698