OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef _CRT_RAND_S | |
6 #define _CRT_RAND_S | |
7 #endif | |
8 | |
9 #include "platform/globals.h" | 5 #include "platform/globals.h" |
10 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_FUCHSIA) |
11 | 7 |
12 #include "bin/crypto.h" | 8 #include "bin/crypto.h" |
13 | 9 |
14 namespace dart { | 10 namespace dart { |
15 namespace bin { | 11 namespace bin { |
16 | 12 |
17 bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) { | 13 bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) { |
18 uint32_t num; | 14 uint32_t num; |
19 intptr_t read = 0; | 15 intptr_t read = 0; |
20 while (read < count) { | 16 while (read < count) { |
21 if (rand_s(&num) != 0) { | 17 if (rand_r(&num) != 0) { |
22 return false; | 18 return false; |
23 } | 19 } |
24 for (int i = 0; i < 4 && read < count; i++) { | 20 for (int i = 0; i < 4 && read < count; i++) { |
25 buffer[read] = num >> (i * 8); | 21 buffer[read] = num >> (i * 8); |
26 read++; | 22 read++; |
27 } | 23 } |
28 } | 24 } |
29 return true; | 25 return true; |
30 } | 26 } |
31 | 27 |
32 } // namespace bin | 28 } // namespace bin |
33 } // namespace dart | 29 } // namespace dart |
34 | 30 |
35 #endif // defined(TARGET_OS_WINDOWS) | 31 #endif // defined(TARGET_OS_FUCHSIA) |
OLD | NEW |