Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: base/rand_util_posix.cc

Issue 3800003: Moving GUID generation from base to chrome/browser/guid* (Closed)
Patch Set: Additional comment. Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008 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 #include "base/rand_util_c.h" 6 #include "base/rand_util_c.h"
7 7
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <unistd.h> 10 #include <unistd.h>
11 11
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/stringprintf.h"
16 15
17 namespace { 16 namespace {
18 17
19 // We keep the file descriptor for /dev/urandom around so we don't need to 18 // We keep the file descriptor for /dev/urandom around so we don't need to
20 // reopen it (which is expensive), and since we may not even be able to reopen 19 // reopen it (which is expensive), and since we may not even be able to reopen
21 // it if we are later put in a sandbox. This class wraps the file descriptor so 20 // it if we are later put in a sandbox. This class wraps the file descriptor so
22 // we can use LazyInstance to handle opening it on the first access. 21 // we can use LazyInstance to handle opening it on the first access.
23 class URandomFd { 22 class URandomFd {
24 public: 23 public:
25 URandomFd() { 24 URandomFd() {
(...skipping 22 matching lines...) Expand all
48 47
49 int urandom_fd = g_urandom_fd.Pointer()->fd(); 48 int urandom_fd = g_urandom_fd.Pointer()->fd();
50 bool success = file_util::ReadFromFD(urandom_fd, 49 bool success = file_util::ReadFromFD(urandom_fd,
51 reinterpret_cast<char*>(&number), 50 reinterpret_cast<char*>(&number),
52 sizeof(number)); 51 sizeof(number));
53 CHECK(success); 52 CHECK(success);
54 53
55 return number; 54 return number;
56 } 55 }
57 56
58 // TODO(cmasone): Once we're comfortable this works, migrate Windows code to
59 // use this as well.
60 std::string RandomBytesToGUIDString(const uint64 bytes[2]) {
61 return StringPrintf("%08X-%04X-%04X-%04X-%012llX",
62 static_cast<unsigned int>(bytes[0] >> 32),
63 static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff),
64 static_cast<unsigned int>(bytes[0] & 0x0000ffff),
65 static_cast<unsigned int>(bytes[1] >> 48),
66 bytes[1] & 0x0000ffffffffffffULL);
67 }
68
69 std::string GenerateGUID() {
70 uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() };
71 return RandomBytesToGUIDString(sixteen_bytes);
72 }
73
74 } // namespace base 57 } // namespace base
75 58
76 int GetUrandomFD(void) { 59 int GetUrandomFD(void) {
77 return g_urandom_fd.Pointer()->fd(); 60 return g_urandom_fd.Pointer()->fd();
78 } 61 }
OLDNEW
« no previous file with comments | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698