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
3 // found in the LICENSE file.
5 #include "base/rand_util.h"
7 #include <nacl/nacl_random.h>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
14 void GetRandomBytes(void* output
, size_t num_bytes
) {
15 char* output_ptr
= static_cast<char*>(output
);
16 while (num_bytes
> 0) {
18 const int error
= nacl_secure_random(output_ptr
, num_bytes
, &nread
);
20 CHECK_LE(nread
, num_bytes
);
30 // NOTE: This function must be cryptographically secure. http://crbug.com/140076
33 GetRandomBytes(&result
, sizeof(result
));
37 void RandBytes(void* output
, size_t output_length
) {
38 GetRandomBytes(output
, output_length
);