Chromium Code Reviews| 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 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #if defined(WEBRTC_WIN) | 21 #if defined(WEBRTC_WIN) |
| 22 #define WIN32_LEAN_AND_MEAN | 22 #define WIN32_LEAN_AND_MEAN |
| 23 #include <windows.h> | 23 #include <windows.h> |
| 24 #include <ntsecapi.h> | 24 #include <ntsecapi.h> |
| 25 #endif // WEBRTC_WIN | 25 #endif // WEBRTC_WIN |
| 26 #endif // else | 26 #endif // else |
| 27 #endif // FEATURE_ENABLED_SSL | 27 #endif // FEATURE_ENABLED_SSL |
| 28 | 28 |
| 29 #include "webrtc/base/base64.h" | 29 #include "webrtc/base/base64.h" |
| 30 #include "webrtc/base/basictypes.h" | 30 #include "webrtc/base/basictypes.h" |
| 31 #include "webrtc/base/checks.h" | |
| 31 #include "webrtc/base/logging.h" | 32 #include "webrtc/base/logging.h" |
| 32 #include "webrtc/base/timeutils.h" | 33 #include "webrtc/base/timeutils.h" |
| 33 | 34 |
| 34 // Protect against max macro inclusion. | 35 // Protect against max macro inclusion. |
| 35 #undef max | 36 #undef max |
| 36 | 37 |
| 37 namespace rtc { | 38 namespace rtc { |
| 38 | 39 |
| 39 // Base class for RNG implementations. | 40 // Base class for RNG implementations. |
| 40 class RandomGenerator { | 41 class RandomGenerator { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 bool InitRandom(const char* seed, size_t len) { | 209 bool InitRandom(const char* seed, size_t len) { |
| 209 if (!Rng().Init(seed, len)) { | 210 if (!Rng().Init(seed, len)) { |
| 210 LOG(LS_ERROR) << "Failed to init random generator!"; | 211 LOG(LS_ERROR) << "Failed to init random generator!"; |
| 211 return false; | 212 return false; |
| 212 } | 213 } |
| 213 return true; | 214 return true; |
| 214 } | 215 } |
| 215 | 216 |
| 216 std::string CreateRandomString(size_t len) { | 217 std::string CreateRandomString(size_t len) { |
| 217 std::string str; | 218 std::string str; |
| 218 CreateRandomString(len, &str); | 219 RTC_CHECK(CreateRandomString(len, &str)); |
| 219 return str; | 220 return str; |
| 220 } | 221 } |
| 221 | 222 |
| 222 bool CreateRandomString(size_t len, | 223 bool CreateRandomString(size_t len, |
| 223 const char* table, int table_size, | 224 const char* table, int table_size, |
| 224 std::string* str) { | 225 std::string* str) { |
| 225 str->clear(); | 226 str->clear(); |
| 226 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); | 227 std::unique_ptr<uint8_t[]> bytes(new uint8_t[len]); |
| 227 if (!Rng().Generate(bytes.get(), len)) { | 228 if (!Rng().Generate(bytes.get(), len)) { |
| 228 LOG(LS_ERROR) << "Failed to generate random string!"; | 229 LOG(LS_ERROR) << "Failed to generate random string!"; |
| 229 return false; | 230 return false; |
| 230 } | 231 } |
| 231 str->reserve(len); | 232 str->reserve(len); |
| 232 for (size_t i = 0; i < len; ++i) { | 233 for (size_t i = 0; i < len; ++i) { |
| 233 str->push_back(table[bytes[i] % table_size]); | 234 str->push_back(table[bytes[i] % table_size]); |
| 234 } | 235 } |
| 235 return true; | 236 return true; |
| 236 } | 237 } |
| 237 | 238 |
| 238 bool CreateRandomString(size_t len, std::string* str) { | 239 bool CreateRandomString(size_t len, std::string* str) { |
| 239 return CreateRandomString(len, kBase64, 64, str); | 240 return CreateRandomString(len, kBase64, 64, str); |
| 240 } | 241 } |
| 241 | 242 |
| 242 bool CreateRandomString(size_t len, const std::string& table, | 243 bool CreateRandomString(size_t len, const std::string& table, |
| 243 std::string* str) { | 244 std::string* str) { |
| 244 return CreateRandomString(len, table.c_str(), | 245 return CreateRandomString(len, table.c_str(), |
| 245 static_cast<int>(table.size()), str); | 246 static_cast<int>(table.size()), str); |
| 246 } | 247 } |
| 247 | 248 |
| 249 std::string CreateRandomUuid() { | |
| 250 std::string uuid; | |
| 251 RTC_CHECK(CreateRandomUuid(&uuid)); | |
| 252 return uuid; | |
| 253 } | |
| 254 | |
| 248 // Version 4 UUID is of the form: | 255 // Version 4 UUID is of the form: |
| 249 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx | 256 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx |
| 250 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. | 257 // Where 'x' is a hex digit, and 'y' is 8, 9, a or b. |
| 251 std::string CreateRandomUuid() { | 258 bool CreateRandomUuid(std::string *str) { |
| 252 std::string str; | 259 str->clear(); |
| 253 std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]); | 260 std::unique_ptr<uint8_t[]> bytes(new uint8_t[31]); |
| 254 if (!Rng().Generate(bytes.get(), 31)) { | 261 if (!Rng().Generate(bytes.get(), 31)) { |
| 255 LOG(LS_ERROR) << "Failed to generate random string!"; | 262 LOG(LS_ERROR) << "Failed to generate random string!"; |
| 256 return str; | 263 return false; |
| 257 } | 264 } |
| 258 str.reserve(36); | 265 str->reserve(36); |
| 259 for (size_t i = 0; i < 8; ++i) { | 266 for (size_t i = 0; i < 8; ++i) { |
| 260 str.push_back(kHex[bytes[i] % 16]); | 267 str->push_back(kHex[bytes[i] % 16]); |
| 261 } | 268 } |
| 262 str.push_back('-'); | 269 str->push_back('-'); |
| 263 for (size_t i = 8; i < 12; ++i) { | 270 for (size_t i = 8; i < 12; ++i) { |
| 264 str.push_back(kHex[bytes[i] % 16]); | 271 str->push_back(kHex[bytes[i] % 16]); |
| 265 } | 272 } |
| 266 str.push_back('-'); | 273 str->push_back('-'); |
| 267 str.push_back('4'); | 274 str->push_back('4'); |
| 268 for (size_t i = 12; i < 15; ++i) { | 275 for (size_t i = 12; i < 15; ++i) { |
| 269 str.push_back(kHex[bytes[i] % 16]); | 276 str->push_back(kHex[bytes[i] % 16]); |
| 270 } | 277 } |
| 271 str.push_back('-'); | 278 str->push_back('-'); |
| 272 str.push_back(kUuidDigit17[bytes[15] % 4]); | 279 str->push_back(kUuidDigit17[bytes[15] % 4]); |
| 273 for (size_t i = 16; i < 19; ++i) { | 280 for (size_t i = 16; i < 19; ++i) { |
| 274 str.push_back(kHex[bytes[i] % 16]); | 281 str->push_back(kHex[bytes[i] % 16]); |
| 275 } | 282 } |
| 276 str.push_back('-'); | 283 str->push_back('-'); |
| 277 for (size_t i = 19; i < 31; ++i) { | 284 for (size_t i = 19; i < 31; ++i) { |
| 278 str.push_back(kHex[bytes[i] % 16]); | 285 str->push_back(kHex[bytes[i] % 16]); |
| 279 } | 286 } |
| 280 return str; | 287 return true; |
| 281 } | 288 } |
| 282 | 289 |
| 283 uint32_t CreateRandomId() { | 290 uint32_t CreateRandomId() { |
| 284 uint32_t id; | 291 uint32_t id; |
| 285 if (!Rng().Generate(&id, sizeof(id))) { | 292 RTC_CHECK(CreateRandomId(&id)); |
| 286 LOG(LS_ERROR) << "Failed to generate random id!"; | |
| 287 } | |
| 288 return id; | 293 return id; |
| 289 } | 294 } |
| 290 | 295 |
| 296 bool CreateRandomId(uint32_t* id) { | |
| 297 if (!Rng().Generate(id, sizeof(*id))) { | |
| 298 LOG(LS_ERROR) << "Failed to generate random id!"; | |
| 299 return false; | |
| 300 } | |
| 301 return true; | |
| 302 } | |
| 303 | |
| 291 uint64_t CreateRandomId64() { | 304 uint64_t CreateRandomId64() { |
| 292 return static_cast<uint64_t>(CreateRandomId()) << 32 | CreateRandomId(); | 305 uint64_t id; |
| 306 RTC_CHECK(CreateRandomId64(&id)); | |
| 307 return id; | |
| 308 } | |
| 309 | |
| 310 bool CreateRandomId64(uint64_t* id) { | |
| 311 uint32_t high; | |
| 312 uint32_t low; | |
| 313 if (!CreateRandomId(&high) || !CreateRandomId(&low)) { | |
|
joachim
2016/07/01 23:12:29
Maybe get the uint64_t directly from the RNG?
| |
| 314 return false; | |
| 315 } | |
| 316 *id = static_cast<uint64_t>(high) << 32 | low; | |
| 317 return true; | |
| 293 } | 318 } |
| 294 | 319 |
| 295 uint32_t CreateRandomNonZeroId() { | 320 uint32_t CreateRandomNonZeroId() { |
| 296 uint32_t id; | 321 uint32_t id; |
| 297 do { | 322 RTC_CHECK(CreateRandomNonZeroId(&id)); |
| 298 id = CreateRandomId(); | |
| 299 } while (id == 0); | |
| 300 return id; | 323 return id; |
| 301 } | 324 } |
| 302 | 325 |
| 326 bool CreateRandomNonZeroId(uint32_t* id) { | |
| 327 do { | |
| 328 if (!CreateRandomId(id)) { | |
| 329 return false; | |
| 330 } | |
| 331 } while (*id == 0); | |
| 332 return true; | |
| 333 } | |
| 334 | |
| 303 double CreateRandomDouble() { | 335 double CreateRandomDouble() { |
| 304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 336 double value; |
| 305 std::numeric_limits<double>::epsilon()); | 337 RTC_CHECK(CreateRandomDouble(&value)); |
| 338 return value; | |
| 339 } | |
| 340 | |
| 341 bool CreateRandomDouble(double* value) { | |
| 342 uint32_t id; | |
| 343 if (!CreateRandomId(&id)) { | |
| 344 return false; | |
| 345 } | |
| 346 *value = id / (std::numeric_limits<uint32_t>::max() + | |
| 347 std::numeric_limits<double>::epsilon()); | |
| 348 return true; | |
| 306 } | 349 } |
| 307 | 350 |
| 308 } // namespace rtc | 351 } // namespace rtc |
| OLD | NEW |