diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-29 21:03:54 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-29 21:03:54 +0000 |
commit | 29548d8d4f224f681c70e85a7fa0466dff4df92f (patch) | |
tree | 12cd6faa39cb43e879e907f09ab9734156724ce0 /base/rand_util.cc | |
parent | deaa35b7bad1d06f28ed394fb850b4f3dd7bc871 (diff) | |
download | chromium_src-29548d8d4f224f681c70e85a7fa0466dff4df92f.zip chromium_src-29548d8d4f224f681c70e85a7fa0466dff4df92f.tar.gz chromium_src-29548d8d4f224f681c70e85a7fa0466dff4df92f.tar.bz2 |
Add a rand_util method for generating a random string.
We need this function to generate a nonce for MAC cookies.
Review URL: http://codereview.chromium.org/6904118
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@83588 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/rand_util.cc')
-rw-r--r-- | base/rand_util.cc | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/base/rand_util.cc b/base/rand_util.cc index ea6ffd3..d89f8b7 100644 --- a/base/rand_util.cc +++ b/base/rand_util.cc @@ -41,4 +41,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; + } + + return result; +} + } // namespace base |