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