OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #include "bin/crypto.h" | 5 #include "bin/crypto.h" |
6 #include "bin/dartutils.h" | 6 #include "bin/dartutils.h" |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 | 9 |
10 | 10 |
11 void FUNCTION_NAME(Crypto_GetRandomBytes)(Dart_NativeArguments args) { | 11 void FUNCTION_NAME(Crypto_GetRandomBytes)(Dart_NativeArguments args) { |
12 Dart_EnterScope(); | 12 Dart_EnterScope(); |
13 Dart_Handle count_obj = Dart_GetNativeArgument(args, 0); | 13 Dart_Handle count_obj = Dart_GetNativeArgument(args, 0); |
14 int64_t count = 0; | 14 int64_t count = 0; |
15 if (!DartUtils::GetInt64Value(count_obj, &count)) { | 15 if (!DartUtils::GetInt64Value(count_obj, &count)) { |
16 Dart_Handle error = Dart_NewString("Invalid argument, must be an int."); | 16 Dart_Handle error = |
| 17 DartUtils::NewString("Invalid argument, must be an int."); |
17 Dart_ThrowException(error); | 18 Dart_ThrowException(error); |
18 } | 19 } |
19 uint8_t* buffer = new uint8_t[count]; | 20 uint8_t* buffer = new uint8_t[count]; |
20 ASSERT(buffer != NULL); | 21 ASSERT(buffer != NULL); |
21 if (!Crypto::GetRandomBytes(count, buffer)) { | 22 if (!Crypto::GetRandomBytes(count, buffer)) { |
22 delete[] buffer; | 23 delete[] buffer; |
23 Dart_ThrowException(DartUtils::NewDartOSError()); | 24 Dart_ThrowException(DartUtils::NewDartOSError()); |
24 } | 25 } |
25 Dart_Handle result = Dart_NewByteArray(count); | 26 Dart_Handle result = Dart_NewByteArray(count); |
26 if (Dart_IsError(result)) { | 27 if (Dart_IsError(result)) { |
27 delete[] buffer; | 28 delete[] buffer; |
28 Dart_Handle error = Dart_NewString("Failed to allocate storage."); | 29 Dart_Handle error = DartUtils::NewString("Failed to allocate storage."); |
29 Dart_ThrowException(error); | 30 Dart_ThrowException(error); |
30 } | 31 } |
31 Dart_ListSetAsBytes(result, 0, buffer, count); | 32 Dart_ListSetAsBytes(result, 0, buffer, count); |
32 Dart_SetReturnValue(args, result); | 33 Dart_SetReturnValue(args, result); |
33 delete[] buffer; | 34 delete[] buffer; |
34 Dart_ExitScope(); | 35 Dart_ExitScope(); |
35 } | 36 } |
36 | |
OLD | NEW |