rand_util_nacl.cc 684 B

123456789101112131415161718192021222324252627
  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.
  4. #include "base/rand_util.h"
  5. #include <nacl/nacl_random.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/check_op.h"
  9. namespace base {
  10. void RandBytes(void* output, size_t output_length) {
  11. char* output_ptr = static_cast<char*>(output);
  12. while (output_length > 0) {
  13. size_t nread;
  14. const int error = nacl_secure_random(output_ptr, output_length, &nread);
  15. CHECK_EQ(error, 0);
  16. CHECK_LE(nread, output_length);
  17. output_ptr += nread;
  18. output_length -= nread;
  19. }
  20. }
  21. } // namespace base