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

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: Follow bbretw review 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
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/string_util.h"
13 14
14 namespace base { 15 namespace base {
15 16
16 int RandInt(int min, int max) { 17 int RandInt(int min, int max) {
17 DCHECK_LE(min, max); 18 DCHECK_LE(min, max);
18 19
19 uint64 range = static_cast<uint64>(max) - min + 1; 20 uint64 range = static_cast<uint64>(max) - min + 1;
20 int result = min + static_cast<int>(base::RandGenerator(range)); 21 int result = min + static_cast<int>(base::RandGenerator(range));
21 DCHECK(result >= min && result <= max); 22 DCHECK(result >= min && result <= max);
22 return result; 23 return result;
(...skipping 11 matching lines...) Expand all
34 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 35 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
35 DCHECK(result >= 0.0 && result < 1.0); 36 DCHECK(result >= 0.0 && result < 1.0);
36 return result; 37 return result;
37 } 38 }
38 39
39 uint64 RandGenerator(uint64 max) { 40 uint64 RandGenerator(uint64 max) {
40 DCHECK_GT(max, 0ULL); 41 DCHECK_GT(max, 0ULL);
41 return base::RandUint64() % max; 42 return base::RandUint64() % max;
42 } 43 }
43 44
45 void RandBytes(void* output, size_t output_length) {
46 uint64 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, copy_count);
52 }
53 }
54
44 std::string RandBytesAsString(size_t length) { 55 std::string RandBytesAsString(size_t length) {
45 const size_t kBitsPerChar = 8; 56 std::string result;
46 const int kCharsPerInt64 = sizeof(uint64)/sizeof(char); 57 RandBytes(WriteInto(&result, length + 1), length);
47
48 std::string result(length, '\0');
49 uint64 entropy = 0;
50 for (size_t i = 0; i < result.size(); ++i) {
51 if (i % kCharsPerInt64 == 0)
52 entropy = RandUint64();
53 result[i] = static_cast<char>(entropy);
54 entropy >>= kBitsPerChar;
55 }
56
57 return result; 58 return result;
58 } 59 }
59 60
60 } // namespace base 61 } // 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