| 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 RTC_BASE_HELPERS_H_ |
| 12 | #define RTC_BASE_HELPERS_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 |
| 20 | #include "absl/strings/string_view.h" |
| 21 | #include "rtc_base/system/rtc_export.h" |
| 22 |
| 23 | namespace rtc { |
| 24 |
| 25 | // Interface for RNG implementations. |
| 26 | class RandomGenerator { |
| 27 | public: |
| 28 | virtual ~RandomGenerator() {} |
| 29 | virtual bool Init(const void* seed, size_t len) = 0; |
| 30 | virtual bool Generate(void* buf, size_t len) = 0; |
| 31 | }; |
| 32 |
| 33 | // Sets the default random generator as the source of randomness. The default |
| 34 | // source uses the OpenSSL RNG and provides cryptographically secure randomness. |
| 35 | void SetDefaultRandomGenerator(); |
| 36 |
| 37 | // Set a custom random generator. Results produced by CreateRandomXyz |
| 38 | // are cryptographically random iff the output of the supplied generator is |
| 39 | // cryptographically random. |
| 40 | void SetRandomGenerator(std::unique_ptr<RandomGenerator> generator); |
| 41 |
| 42 | // For testing, we can return predictable data. |
| 43 | void SetRandomTestMode(bool test); |
| 44 |
| 45 | // Initializes the RNG, and seeds it with the specified entropy. |
| 46 | bool InitRandom(int seed); |
| 47 | bool InitRandom(const char* seed, size_t len); |
| 48 |
| 49 | // Generates a (cryptographically) random string of the given length. |
| 50 | // We generate base64 values so that they will be printable. |
| 51 | RTC_EXPORT std::string CreateRandomString(size_t length); |
| 52 |
| 53 | // Generates a (cryptographically) random string of the given length. |
| 54 | // We generate base64 values so that they will be printable. |
| 55 | // Return false if the random number generator failed. |
| 56 | RTC_EXPORT bool CreateRandomString(size_t length, std::string* str); |
| 57 |
| 58 | // Generates a (cryptographically) random string of the given length, |
| 59 | // with characters from the given table. Return false if the random |
| 60 | // number generator failed. |
| 61 | // For ease of implementation, the function requires that the table |
| 62 | // size evenly divide 256; otherwise, it returns false. |
| 63 | RTC_EXPORT bool CreateRandomString(size_t length, |
| 64 | absl::string_view table, |
| 65 | std::string* str); |
| 66 |
| 67 | // Generates (cryptographically) random data of the given length. |
| 68 | // Return false if the random number generator failed. |
| 69 | bool CreateRandomData(size_t length, std::string* data); |
| 70 |
| 71 | // Generates a (cryptographically) random UUID version 4 string. |
| 72 | std::string CreateRandomUuid(); |
| 73 |
| 74 | // Generates a random id. |
| 75 | uint32_t CreateRandomId(); |
| 76 |
| 77 | // Generates a 64 bit random id. |
| 78 | RTC_EXPORT uint64_t CreateRandomId64(); |
| 79 |
| 80 | // Generates a random id > 0. |
| 81 | uint32_t CreateRandomNonZeroId(); |
| 82 |
| 83 | // Generates a random double between 0.0 (inclusive) and 1.0 (exclusive). |
| 84 | double CreateRandomDouble(); |
| 85 |
| 86 | // Compute moving average with the given ratio between the previous average |
| 87 | // value and the current value. |
| 88 | double GetNextMovingAverage(double prev_average, double cur, double ratio); |
| 89 |
| 90 | } // namespace rtc |
| 91 |
| 92 | #endif // RTC_BASE_HELPERS_H_ |
| 93 |