henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_HELPERS_H_ |
| 12 | #define WEBRTC_BASE_HELPERS_H_ |
| 13 | |
| 14 | #include <string> |
| 15 | #include "webrtc/base/basictypes.h" |
| 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | // For testing, we can return predictable data. |
| 20 | void SetRandomTestMode(bool test); |
| 21 | |
| 22 | // Initializes the RNG, and seeds it with the specified entropy. |
| 23 | bool InitRandom(int seed); |
| 24 | bool InitRandom(const char* seed, size_t len); |
| 25 | |
| 26 | // Generates a (cryptographically) random string of the given length. |
| 27 | // We generate base64 values so that they will be printable. |
| 28 | // WARNING: could silently fail. Use the version below instead. |
| 29 | std::string CreateRandomString(size_t length); |
| 30 | |
| 31 | // Generates a (cryptographically) random string of the given length. |
| 32 | // We generate base64 values so that they will be printable. |
| 33 | // Return false if the random number generator failed. |
| 34 | bool CreateRandomString(size_t length, std::string* str); |
| 35 | |
| 36 | // Generates a (cryptographically) random string of the given length, |
| 37 | // with characters from the given table. Return false if the random |
| 38 | // number generator failed. |
| 39 | bool CreateRandomString(size_t length, const std::string& table, |
| 40 | std::string* str); |
| 41 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame^] | 42 | // Generates a (cryptographically) random UUID version 4 string. |
| 43 | std::string CreateRandomUuid(); |
| 44 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | // Generates a random id. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 46 | uint32_t CreateRandomId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | |
| 48 | // Generates a 64 bit random id. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 49 | uint64_t CreateRandomId64(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
| 51 | // Generates a random id > 0. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 52 | uint32_t CreateRandomNonZeroId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 53 | |
| 54 | // Generates a random double between 0.0 (inclusive) and 1.0 (exclusive). |
| 55 | double CreateRandomDouble(); |
| 56 | |
| 57 | } // namespace rtc |
| 58 | |
| 59 | #endif // WEBRTC_BASE_HELPERS_H_ |