Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: base/rand_util.cc

Issue 6873156: Move crypto_helpers from sync to base (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Correct doc and test Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/rand_util.h" 5 #include "base/rand_util.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <limits> 9 #include <limits>
10 10
(...skipping 23 matching lines...) Expand all
34 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 34 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
35 DCHECK(result >= 0.0 && result < 1.0); 35 DCHECK(result >= 0.0 && result < 1.0);
36 return result; 36 return result;
37 } 37 }
38 38
39 uint64 RandGenerator(uint64 max) { 39 uint64 RandGenerator(uint64 max) {
40 DCHECK_GT(max, 0ULL); 40 DCHECK_GT(max, 0ULL);
41 return base::RandUint64() % max; 41 return base::RandUint64() % max;
42 } 42 }
43 43
44 void RandBytes(void* output, size_t output_length) {
45 uint64 random_int;
46 const char* random_int_bytes = reinterpret_cast<const char*>(&random_int);
47 size_t random_int_size = sizeof(random_int);
48 for (size_t i = 0; i < output_length; i += random_int_size) {
49 random_int = base::RandUint64();
50 size_t copy_count = std::min(output_length - i, random_int_size);
51 memcpy(((uint8*)output) + i, random_int_bytes, copy_count);
52 }
abarth-chromium 2011/05/02 18:23:20 I'd recommend re-implementing RandBytesAsString in
53 }
54
44 std::string RandBytesAsString(size_t length) { 55 std::string RandBytesAsString(size_t length) {
45 const size_t kBitsPerChar = 8; 56 const size_t kBitsPerChar = 8;
46 const int kCharsPerInt64 = sizeof(uint64)/sizeof(char); 57 const int kCharsPerInt64 = sizeof(uint64)/sizeof(char);
47 58
48 std::string result(length, '\0'); 59 std::string result(length, '\0');
49 uint64 entropy = 0; 60 uint64 entropy = 0;
50 for (size_t i = 0; i < result.size(); ++i) { 61 for (size_t i = 0; i < result.size(); ++i) {
51 if (i % kCharsPerInt64 == 0) 62 if (i % kCharsPerInt64 == 0)
52 entropy = RandUint64(); 63 entropy = RandUint64();
53 result[i] = static_cast<char>(entropy); 64 result[i] = static_cast<char>(entropy);
54 entropy >>= kBitsPerChar; 65 entropy >>= kBitsPerChar;
55 } 66 }
56 67
57 return result; 68 return result;
58 } 69 }
59 70
60 } // namespace base 71 } // namespace base
OLDNEW
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698