OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 | 23 |
24 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 24 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
25 uint64 RandUint64() { | 25 uint64 RandUint64() { |
26 uint32 first_half = RandUint32(); | 26 uint32 first_half = RandUint32(); |
27 uint32 second_half = RandUint32(); | 27 uint32 second_half = RandUint32(); |
28 return (static_cast<uint64>(first_half) << 32) + second_half; | 28 return (static_cast<uint64>(first_half) << 32) + second_half; |
29 } | 29 } |
30 | 30 |
31 void RandBytes(void* output, size_t output_length) { | |
32 uint64 random_int; | |
bradn
2014/01/22 21:56:12
Any reason not to use:
CryptGenRandom ?
http://msd
wtc
2014/01/22 22:29:09
There is a better Windows function: RtlGenRandom.
| |
33 const size_t random_int_size = sizeof(random_int); | |
34 for (size_t i = 0; i < output_length; i += random_int_size) { | |
35 random_int = base::RandUint64(); | |
36 size_t copy_count = std::min(output_length - i, random_int_size); | |
wtc
2014/01/22 22:29:09
|copy_count| can also be 'const'. (I don't do this
DaleCurtis
2014/01/22 22:54:14
Done.
| |
37 memcpy(((uint8*)output) + i, &random_int, copy_count); | |
38 } | |
39 } | |
40 | |
31 } // namespace base | 41 } // namespace base |
OLD | NEW |