Chromium Code Reviews| 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]; | |
|
Søren Gjesse
2012/10/22 11:13:02
Shouldn't we check for NULL here?
Anders Johnsen
2012/10/22 12:18:57
Done.
| |
| 20 if (!Crypto::GetRandomBytes(count, buffer)) { | |
| 21 delete[] buffer; | |
| 22 Dart_Handle error = Dart_NewString("Failed to get random bytes."); | |
|
Søren Gjesse
2012/10/22 11:13:02
Maybe this should be an OSError is errno/GetLastEr
Anders Johnsen
2012/10/22 12:18:57
Done.
| |
| 23 Dart_ThrowException(error); | |
| 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 |