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 | |
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')
-rw-r--r-- | base/rand_util.cc | 23 | ||||
-rw-r--r-- | base/rand_util.h | 9 | ||||
-rw-r--r-- | base/rand_util_unittest.cc | 13 |
3 files changed, 33 insertions, 12 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; } diff --git a/base/rand_util.h b/base/rand_util.h index d10cc8b..6bfbcb4 100644 --- a/base/rand_util.h +++ b/base/rand_util.h @@ -33,7 +33,14 @@ BASE_API double RandDouble(); // the range [0, 1). Thread-safe. BASE_API double BitsToOpenEndedUnitInterval(uint64 bits); -// Returns a random string of the specified length. +// Fills |output_length| bytes of |output| with cryptographically strong random +// data. +BASE_API void RandBytes(void* output, size_t output_length); + +// Fills a string of length |length| with with cryptographically strong random +// data and returns it. +// +// Not that this is a variation of |RandBytes| with a different return type. BASE_API std::string RandBytesAsString(size_t length); } // namespace base diff --git a/base/rand_util_unittest.cc b/base/rand_util_unittest.cc index 3bdb815..d7fa37a 100644 --- a/base/rand_util_unittest.cc +++ b/base/rand_util_unittest.cc @@ -28,6 +28,19 @@ TEST(RandUtilTest, RandDouble) { EXPECT_LE(0.0, number); } +TEST(RandUtilTest, RandBytes) { + const size_t buffer_size = 145; + char buffer[buffer_size]; + memset(buffer, 0, buffer_size); + base::RandBytes(buffer, buffer_size); + char accumulator = 0; + for(size_t i = 0; i < buffer_size; ++i) + accumulator |= buffer[i]; + // In theory this test can fail, but it won't before the universe dies of + // heat death. + EXPECT_NE(0, accumulator); +} + TEST(RandUtilTest, RandBytesAsString) { std::string random_string = base::RandBytesAsString(0); EXPECT_EQ(0U, random_string.size()); |