147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/* 247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * Copyright 2004 The WebRTC Project Authors. All rights reserved. 347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * 447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * Use of this source code is governed by a BSD-style license 547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * that can be found in the LICENSE file in the root of the source 647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * tree. An additional intellectual property rights grant can be found 747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * in the file PATENTS. All contributing project authors may 847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org * be found in the AUTHORS file in the root of the source tree. 947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */ 1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef WEBRTC_BASE_HELPERS_H_ 1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WEBRTC_BASE_HELPERS_H_ 1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <string> 1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/basictypes.h" 1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc { 1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// For testing, we can return predictable data. 2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid SetRandomTestMode(bool test); 2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Initializes the RNG, and seeds it with the specified entropy. 2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool InitRandom(int seed); 2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool InitRandom(const char* seed, size_t len); 2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a (cryptographically) random string of the given length. 2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// We generate base64 values so that they will be printable. 2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// WARNING: could silently fail. Use the version below instead. 2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string CreateRandomString(size_t length); 3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a (cryptographically) random string of the given length. 3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// We generate base64 values so that they will be printable. 3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Return false if the random number generator failed. 3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool CreateRandomString(size_t length, std::string* str); 3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a (cryptographically) random string of the given length, 3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// with characters from the given table. Return false if the random 3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// number generator failed. 3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool CreateRandomString(size_t length, const std::string& table, 4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org std::string* str); 4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a random id. 4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orguint32 CreateRandomId(); 4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a 64 bit random id. 4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orguint64 CreateRandomId64(); 4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a random id > 0. 4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orguint32 CreateRandomNonZeroId(); 5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Generates a random double between 0.0 (inclusive) and 1.0 (exclusive). 5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgdouble CreateRandomDouble(); 5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org} // namespace rtc 5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org 5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif // WEBRTC_BASE_HELPERS_H_ 57