OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/helpers.h" | 11 #include "webrtc/base/helpers.h" |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
| 14 #include <memory> |
14 | 15 |
15 #if defined(FEATURE_ENABLE_SSL) | 16 #if defined(FEATURE_ENABLE_SSL) |
16 #include "webrtc/base/sslconfig.h" | 17 #include "webrtc/base/sslconfig.h" |
17 #if defined(SSL_USE_OPENSSL) | 18 #if defined(SSL_USE_OPENSSL) |
18 #include <openssl/rand.h> | 19 #include <openssl/rand.h> |
19 #else | 20 #else |
20 #if defined(WEBRTC_WIN) | 21 #if defined(WEBRTC_WIN) |
21 #define WIN32_LEAN_AND_MEAN | 22 #define WIN32_LEAN_AND_MEAN |
22 #include <windows.h> | 23 #include <windows.h> |
23 #include <ntsecapi.h> | 24 #include <ntsecapi.h> |
24 #endif // WEBRTC_WIN | 25 #endif // WEBRTC_WIN |
25 #endif // else | 26 #endif // else |
26 #endif // FEATURE_ENABLED_SSL | 27 #endif // FEATURE_ENABLED_SSL |
27 | 28 |
28 #include "webrtc/base/base64.h" | 29 #include "webrtc/base/base64.h" |
29 #include "webrtc/base/basictypes.h" | 30 #include "webrtc/base/basictypes.h" |
30 #include "webrtc/base/logging.h" | 31 #include "webrtc/base/logging.h" |
31 #include "webrtc/base/scoped_ptr.h" | |
32 #include "webrtc/base/timeutils.h" | 32 #include "webrtc/base/timeutils.h" |
33 | 33 |
34 // Protect against max macro inclusion. | 34 // Protect against max macro inclusion. |
35 #undef max | 35 #undef max |
36 | 36 |
37 namespace rtc { | 37 namespace rtc { |
38 | 38 |
39 // Base class for RNG implementations. | 39 // Base class for RNG implementations. |
40 class RandomGenerator { | 40 class RandomGenerator { |
41 public: | 41 public: |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | 174 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
175 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | 175 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
176 | 176 |
177 static const char kHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | 177 static const char kHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', |
178 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | 178 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
179 | 179 |
180 static const char kUuidDigit17[4] = {'8', '9', 'a', 'b'}; | 180 static const char kUuidDigit17[4] = {'8', '9', 'a', 'b'}; |
181 | 181 |
182 // This round about way of creating a global RNG is to safe-guard against | 182 // This round about way of creating a global RNG is to safe-guard against |
183 // indeterminant static initialization order. | 183 // indeterminant static initialization order. |
184 scoped_ptr<RandomGenerator>& GetGlobalRng() { | 184 std::unique_ptr<RandomGenerator>& GetGlobalRng() { |
185 RTC_DEFINE_STATIC_LOCAL(scoped_ptr<RandomGenerator>, global_rng, | 185 RTC_DEFINE_STATIC_LOCAL(std::unique_ptr<RandomGenerator>, global_rng, |
186 (new SecureRandomGenerator())); | 186 (new SecureRandomGenerator())); |
187 return global_rng; | 187 return global_rng; |
188 } | 188 } |
189 | 189 |
190 RandomGenerator& Rng() { | 190 RandomGenerator& Rng() { |
191 return *GetGlobalRng(); | 191 return *GetGlobalRng(); |
192 } | 192 } |
193 | 193 |
194 } // namespace | 194 } // namespace |
195 | 195 |
(...skipping 20 matching lines...) Expand all Loading... |
216 std::string CreateRandomString(size_t len) { | 216 std::string CreateRandomString(size_t len) { |
217 std::string str; | 217 std::string str; |
218 CreateRandomString(len, &str); | 218 CreateRandomString(len, &str); |
219 return str; | 219 return str; |
220 } | 220 } |
221 | 221 |
222 bool CreateRandomString(size_t len, | 222 bool CreateRandomString(size_t len, |
223 const char* table, int table_size, | 223 const char* table, int table_size, |
224 std::string* str) { | 224 std::string* str) { |
225 str->clear(); | 225 str->clear(); |
226 scoped_ptr<uint8_t[]> bytes(new uint8_t[len]); | 226 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); |
227 if (!Rng().Generate(bytes.get(), len)) { | 227 if (!Rng().Generate(bytes.get(), len)) { |
228 LOG(LS_ERROR) << "Failed to generate random string!"; | 228 LOG(LS_ERROR) << "Failed to generate random string!"; |
229 return false; | 229 return false; |
230 } | 230 } |
231 str->reserve(len); | 231 str->reserve(len); |
232 for (size_t i = 0; i < len; ++i) { | 232 for (size_t i = 0; i < len; ++i) { |
233 str->push_back(table[bytes[i] % table_size]); | 233 str->push_back(table[bytes[i] % table_size]); |
234 } | 234 } |
235 return true; | 235 return true; |
236 } | 236 } |
237 | 237 |
238 bool CreateRandomString(size_t len, std::string* str) { | 238 bool CreateRandomString(size_t len, std::string* str) { |
239 return CreateRandomString(len, kBase64, 64, str); | 239 return CreateRandomString(len, kBase64, 64, str); |
240 } | 240 } |
241 | 241 |
242 bool CreateRandomString(size_t len, const std::string& table, | 242 bool CreateRandomString(size_t len, const std::string& table, |
243 std::string* str) { | 243 std::string* str) { |
244 return CreateRandomString(len, table.c_str(), | 244 return CreateRandomString(len, table.c_str(), |
245 static_cast<int>(table.size()), str); | 245 static_cast<int>(table.size()), str); |
246 } | 246 } |
247 | 247 |
248 // Version 4 UUID is of the form: | 248 // Version 4 UUID is of the form: |
249 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx | 249 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx |
250 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. | 250 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. |
251 std::string CreateRandomUuid() { | 251 std::string CreateRandomUuid() { |
252 std::string str; | 252 std::string str; |
253 scoped_ptr<uint8_t[]> bytes(new uint8_t[31]); | 253 std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]); |
254 if (!Rng().Generate(bytes.get(), 31)) { | 254 if (!Rng().Generate(bytes.get(), 31)) { |
255 LOG(LS_ERROR) << "Failed to generate random string!"; | 255 LOG(LS_ERROR) << "Failed to generate random string!"; |
256 return str; | 256 return str; |
257 } | 257 } |
258 str.reserve(36); | 258 str.reserve(36); |
259 for (size_t i = 0; i < 8; ++i) { | 259 for (size_t i = 0; i < 8; ++i) { |
260 str.push_back(kHex[bytes[i] % 16]); | 260 str.push_back(kHex[bytes[i] % 16]); |
261 } | 261 } |
262 str.push_back('-'); | 262 str.push_back('-'); |
263 for (size_t i = 8; i < 12; ++i) { | 263 for (size_t i = 8; i < 12; ++i) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 } while (id == 0); | 299 } while (id == 0); |
300 return id; | 300 return id; |
301 } | 301 } |
302 | 302 |
303 double CreateRandomDouble() { | 303 double CreateRandomDouble() { |
304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
305 std::numeric_limits<double>::epsilon()); | 305 std::numeric_limits<double>::epsilon()); |
306 } | 306 } |
307 | 307 |
308 } // namespace rtc | 308 } // namespace rtc |
OLD | NEW |