Chromium Code Reviews| Index: base/rand_util.cc |
| diff --git a/base/rand_util.cc b/base/rand_util.cc |
| index d89f8b725080d3912944b5b7bc60c19096c683ae..58c33263d290dc7b7db367d804fe6bfbc6197ec8 100644 |
| --- a/base/rand_util.cc |
| +++ b/base/rand_util.cc |
| @@ -10,6 +10,7 @@ |
| #include "base/basictypes.h" |
| #include "base/logging.h" |
| +#include "base/string_util.h" |
| namespace base { |
| @@ -41,19 +42,20 @@ uint64 RandGenerator(uint64 max) { |
| return base::RandUint64() % max; |
| } |
| -std::string RandBytesAsString(size_t length) { |
| - const size_t kBitsPerChar = 8; |
| - const int kCharsPerInt64 = sizeof(uint64)/sizeof(char); |
| - |
| - std::string result(length, '\0'); |
| - uint64 entropy = 0; |
| - for (size_t i = 0; i < result.size(); ++i) { |
| - if (i % kCharsPerInt64 == 0) |
| - entropy = RandUint64(); |
| - result[i] = static_cast<char>(entropy); |
| - entropy >>= kBitsPerChar; |
| +void RandBytes(void* output, size_t output_length) { |
| + uint64 random_int; |
| + const char* random_int_bytes = reinterpret_cast<const char*>(&random_int); |
|
agl
2011/05/03 14:53:40
nit: you don't need this. memcpy takes a const voi
|
| + size_t random_int_size = sizeof(random_int); |
| + for (size_t i = 0; i < output_length; i += random_int_size) { |
| + random_int = base::RandUint64(); |
| + size_t copy_count = std::min(output_length - i, random_int_size); |
| + memcpy(((uint8*)output) + i, random_int_bytes, copy_count); |
| } |
| +} |
| +std::string RandBytesAsString(size_t length) { |
| + std::string result; |
| + RandBytes(WriteInto(&result, length + 1), length); |
| return result; |
| } |