OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/rand_util.h" | 5 #include "base/rand_util.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 ~URandomFd() { | 28 ~URandomFd() { |
29 close(fd_); | 29 close(fd_); |
30 } | 30 } |
31 | 31 |
32 int fd() const { return fd_; } | 32 int fd() const { return fd_; } |
33 | 33 |
34 private: | 34 private: |
35 int fd_; | 35 int fd_; |
36 }; | 36 }; |
37 | 37 |
38 base::LazyInstance<URandomFd> g_urandom_fd = LAZY_INSTANCE_INITIALIZER; | 38 base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; |
39 | 39 |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 namespace base { | 42 namespace base { |
43 | 43 |
44 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 44 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
45 uint64 RandUint64() { | 45 uint64 RandUint64() { |
46 uint64 number; | 46 uint64 number; |
47 | 47 |
48 int urandom_fd = g_urandom_fd.Pointer()->fd(); | 48 int urandom_fd = g_urandom_fd.Pointer()->fd(); |
49 bool success = file_util::ReadFromFD(urandom_fd, | 49 bool success = file_util::ReadFromFD(urandom_fd, |
50 reinterpret_cast<char*>(&number), | 50 reinterpret_cast<char*>(&number), |
51 sizeof(number)); | 51 sizeof(number)); |
52 CHECK(success); | 52 CHECK(success); |
53 | 53 |
54 return number; | 54 return number; |
55 } | 55 } |
56 | 56 |
57 int GetUrandomFD(void) { | 57 int GetUrandomFD(void) { |
58 return g_urandom_fd.Pointer()->fd(); | 58 return g_urandom_fd.Pointer()->fd(); |
59 } | 59 } |
60 | 60 |
61 } // namespace base | 61 } // namespace base |
OLD | NEW |