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; |
+} |