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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 bool CreateRandomData(size_t length, std::string* data) { |
| 249 data->resize(length); |
| 250 // std::string is guaranteed to use contiguous memory in c++11 so we can |
| 251 // safely write directly to it. |
| 252 return Rng().Generate(&data->at(0), length); |
| 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 std::string CreateRandomUuid() { |
252 std::string str; | 259 std::string str; |
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 str; |
257 } | 264 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 } while (id == 0); | 306 } while (id == 0); |
300 return id; | 307 return id; |
301 } | 308 } |
302 | 309 |
303 double CreateRandomDouble() { | 310 double CreateRandomDouble() { |
304 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + | 311 return CreateRandomId() / (std::numeric_limits<uint32_t>::max() + |
305 std::numeric_limits<double>::epsilon()); | 312 std::numeric_limits<double>::epsilon()); |
306 } | 313 } |
307 | 314 |
308 } // namespace rtc | 315 } // namespace rtc |
OLD | NEW |