|
reSIProcate/rutil
9694
|
00001 #if !defined(RESIP_RANDOM_HXX) 00002 #define RESIP_RANDOM_HXX 00003 00004 #include "rutil/Mutex.hxx" 00005 #include "rutil/Data.hxx" 00006 #include "rutil/ThreadIf.hxx" // for ThreadLocalStorage 00007 #include <cassert> 00008 struct random_data; 00009 00014 // #define RESIP_RANDOM_WIN32_RTL 1 00015 00021 // #define RESIP_RANDOM_THREAD_MUTEX 1 00022 00028 // #define RESIP_RANDOM_THREAD_LOCAL 1 00029 00041 namespace resip 00042 { 00043 00049 class Random 00050 { 00051 public: 00052 static unsigned getSimpleSeed(); 00053 static void initialize(); 00054 00055 enum {maxLength = 512}; 00056 00057 static Data getRandom(unsigned int numBytes); 00058 static Data getRandomHex(unsigned int numBytes); // actual length is 2*numBytes 00059 static Data getRandomBase64(unsigned int numBytes); // actual length is 1.5*numBytes 00060 00061 static Data getCryptoRandom(unsigned int numBytes); 00062 static Data getCryptoRandomHex(unsigned int numBytes); // actual length is 2*numBytes 00063 static Data getCryptoRandomBase64(unsigned int numBytes); // actual length is 1.5*numBytes 00064 00065 static void getCryptoRandom(unsigned char* buf, unsigned int numBytes); 00066 00078 static Data getVersion4UuidUrn(); 00079 00084 static int getRandom(); 00085 static int getCryptoRandom(); 00086 00087 static const char* getImplName(); 00088 00089 private: 00090 static Mutex mMutex; 00091 static bool mIsInitialized; 00092 00093 #ifdef WIN32 00094 // ensure each thread is initialized since windows requires you to call srand for each thread 00096 class Initializer 00097 { 00098 public: 00099 Initializer(); 00100 ~Initializer(); 00101 void setInitialized(); 00102 bool isInitialized(); 00103 00104 private: 00105 DWORD mThreadStorage; 00106 }; 00107 static Initializer mInitializer; 00108 00109 #ifdef RESIP_RANDOM_WIN32_RTL 00110 static BOOLEAN (APIENTRY *RtlGenRandom)(void*, ULONG); 00111 #endif 00112 #endif // WIN32 00113 #ifdef RESIP_RANDOM_THREAD_LOCAL 00114 static ThreadIf::TlsKey sRandomStateKey; 00115 #endif 00116 #ifdef RESIP_RANDOM_THREAD_MUTEX 00117 static struct random_data* sRandomState; 00118 // we re-use the initialization mutex 00119 #endif 00120 }; 00121 00122 } 00123 00124 #endif 00125 00126 /* ==================================================================== 00127 * The Vovida Software License, Version 1.0 00128 * 00129 * Copyright (c) 2005. All rights reserved. 00130 * 00131 * Redistribution and use in source and binary forms, with or without 00132 * modification, are permitted provided that the following conditions 00133 * are met: 00134 * 00135 * 1. Redistributions of source code must retain the above copyright 00136 * notice, this list of conditions and the following disclaimer. 00137 * 00138 * 2. Redistributions in binary form must reproduce the above copyright 00139 * notice, this list of conditions and the following disclaimer in 00140 * the documentation and/or other materials provided with the 00141 * distribution. 00142 * 00143 * 3. The names "VOCAL", "Vovida Open Communication Application Library", 00144 * and "Vovida Open Communication Application Library (VOCAL)" must 00145 * not be used to endorse or promote products derived from this 00146 * software without prior written permission. For written 00147 * permission, please contact vocal@vovida.org. 00148 * 00149 * 4. Products derived from this software may not be called "VOCAL", nor 00150 * may "VOCAL" appear in their name, without prior written 00151 * permission of Vovida Networks, Inc. 00152 * 00153 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 00154 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00155 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND 00156 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA 00157 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES 00158 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, 00159 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00160 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00161 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00162 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00163 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00164 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 00165 * DAMAGE. 00166 * 00167 * ==================================================================== 00168 * 00169 * This software consists of voluntary contributions made by Vovida 00170 * Networks, Inc. and many individuals on behalf of Vovida Networks, 00171 * Inc. For more information on Vovida Networks, Inc., please see 00172 * <http://www.vovida.org/>. 00173 * 00174 * vi: set shiftwidth=3 expandtab: 00175 */
1.7.5.1