diff options
author | qsr@google.com <qsr@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-05 08:46:11 +0000 |
---|---|---|
committer | qsr@google.com <qsr@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-05 08:46:11 +0000 |
commit | 51a018181c93b3c190146432805836155d69effa (patch) | |
tree | a9ed7d4a2acdbfad83f4c3063250ef283b15405b /base/rand_util.cc | |
parent | 1817055ea2667eda23f5d53d623b3a547a7d19ee (diff) | |
download | chromium_src-51a018181c93b3c190146432805836155d69effa.zip chromium_src-51a018181c93b3c190146432805836155d69effa.tar.gz chromium_src-51a018181c93b3c190146432805836155d69effa.tar.bz2 |
Move crypto_helpers from sync to crypto
crypto_helpers only depends on resources in base and is used by sync and
password_manager.
BUG=
TEST=
Review URL: http://codereview.chromium.org/6873156
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84223 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/rand_util.cc')
-rw-r--r-- | base/rand_util.cc | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/base/rand_util.cc b/base/rand_util.cc index b823fa0..4140e9a 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 { @@ -47,19 +48,19 @@ 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; + 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, copy_count); } +} +std::string RandBytesAsString(size_t length) { + std::string result; + RandBytes(WriteInto(&result, length + 1), length); return result; } |