Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : // Copyright (c) 2008 The Chromium Authors. All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : #include "base/rand_util.h"
8 :
9 : #include <fcntl.h>
10 : #include <unistd.h>
11 :
12 : #include "base/file_util.h"
13 : #include "base/logging.h"
14 :
15 : namespace base {
16 :
17 15 : uint64_t RandUint64() {
18 : uint64_t number;
19 :
20 15 : int urandom_fd = open("/dev/urandom", O_RDONLY);
21 15 : CHECK(urandom_fd >= 0);
22 : bool success = file_util::ReadFromFD(urandom_fd,
23 : reinterpret_cast<char*>(&number),
24 15 : sizeof(number));
25 15 : CHECK(success);
26 15 : close(urandom_fd);
27 :
28 15 : return number;
29 : }
30 :
31 : } // namespace base
|