reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Random.hxx
Go to the documentation of this file.
1#if !defined(RESIP_RANDOM_HXX)
2#define RESIP_RANDOM_HXX
3
4#include "rutil/Mutex.hxx"
5#include "rutil/Data.hxx"
6#include "rutil/ThreadIf.hxx" // for ThreadLocalStorage
7#include "rutil/ResipAssert.h"
8struct random_data;
9
18#define RESIP_RANDOM_WIN32_RTL 1
19
25// #define RESIP_RANDOM_THREAD_MUTEX 1
26
32// #define RESIP_RANDOM_THREAD_LOCAL 1
33
45namespace resip
46{
47
53class Random
54{
55 public:
56 static unsigned getSimpleSeed();
57 static void initialize();
58
59 enum {maxLength = 512};
60
61 static Data getRandom(unsigned int numBytes);
62 static Data getRandomHex(unsigned int numBytes); // actual length is 2*numBytes
63 static Data getRandomBase64(unsigned int numBytes); // actual length is 1.5*numBytes
64
65 static Data getCryptoRandom(unsigned int numBytes);
66 static Data getCryptoRandomHex(unsigned int numBytes); // actual length is 2*numBytes
67 static Data getCryptoRandomBase64(unsigned int numBytes); // actual length is 1.5*numBytes
68
69 static void getCryptoRandom(unsigned char* buf, unsigned int numBytes);
70
82 static Data getVersion4UuidUrn();
83
88 static int getRandom();
89 static int getCryptoRandom();
90
91 static const char* getImplName();
92
93 private:
94 static Mutex mMutex;
95 static bool mIsInitialized;
96
97#ifdef WIN32
98 // ensure each thread is initialized since windows requires you to call srand for each thread
100 class Initializer
101 {
102 public:
103 Initializer();
104 ~Initializer();
105 void setInitialized();
106 bool isInitialized();
107
108 private:
110 };
111 static Initializer mInitializer;
112
113#ifdef RESIP_RANDOM_WIN32_RTL
114 static BOOLEAN (APIENTRY *RtlGenRandom)(void*, ULONG);
115#endif
116#endif // WIN32
117#ifdef RESIP_RANDOM_THREAD_LOCAL
118 static ThreadIf::TlsKey sRandomStateKey;
119#endif
120#ifdef RESIP_RANDOM_THREAD_MUTEX
121 static struct random_data* sRandomState;
122 // we re-use the initialization mutex
123#endif
124};
125
126}
127
128#endif
129
130/* ====================================================================
131 * The Vovida Software License, Version 1.0
132 *
133 * Copyright (c) 2005. All rights reserved.
134 *
135 * Redistribution and use in source and binary forms, with or without
136 * modification, are permitted provided that the following conditions
137 * are met:
138 *
139 * 1. Redistributions of source code must retain the above copyright
140 * notice, this list of conditions and the following disclaimer.
141 *
142 * 2. Redistributions in binary form must reproduce the above copyright
143 * notice, this list of conditions and the following disclaimer in
144 * the documentation and/or other materials provided with the
145 * distribution.
146 *
147 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
148 * and "Vovida Open Communication Application Library (VOCAL)" must
149 * not be used to endorse or promote products derived from this
150 * software without prior written permission. For written
151 * permission, please contact vocal@vovida.org.
152 *
153 * 4. Products derived from this software may not be called "VOCAL", nor
154 * may "VOCAL" appear in their name, without prior written
155 * permission of Vovida Networks, Inc.
156 *
157 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
158 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
159 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
160 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
161 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
162 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
163 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
164 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
165 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
166 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
167 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
168 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
169 * DAMAGE.
170 *
171 * ====================================================================
172 *
173 * This software consists of voluntary contributions made by Vovida
174 * Networks, Inc. and many individuals on behalf of Vovida Networks,
175 * Inc. For more information on Vovida Networks, Inc., please see
176 * <http://www.vovida.org/>.
177 *
178 * vi: set shiftwidth=3 expandtab:
179 */
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
A static class that wraps the random-number generation code of your platform.
Definition Random.hxx:54
static Data getCryptoRandomBase64(unsigned int numBytes)
Definition Random.cxx:400
static int getRandom()
Returns a postive integer (31 bits) of randomness.
Definition Random.cxx:261
static Data getVersion4UuidUrn()
Returns a version 4 (random) UUID as defined in RFC 4122.
Definition Random.cxx:436
static int getCryptoRandom()
Definition Random.cxx:330
static const char * getImplName()
Definition Random.cxx:79
static Data getCryptoRandomHex(unsigned int numBytes)
Definition Random.cxx:394
static void initialize()
Definition Random.cxx:125
static Data getRandomHex(unsigned int numBytes)
Definition Random.cxx:382
static Data getRandomBase64(unsigned int numBytes)
Definition Random.cxx:388
static unsigned getSimpleSeed()
Key goal is to make sure that each thread has distinct seed.
Definition Random.cxx:102
pthread_key_t TlsKey
Definition ThreadIf.hxx:85
dcm! – add UnusedChecking(_enum) below;
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
void list()
Definition userAdmin.cxx:66