nsresult nsSceneTracker::GenerateID (nsACString& aString) { // Ranges: ['0' .. '9'] = [0x30, 0x39], 10 elements // ['A' .. 'Z'] = [0x41, 0x5a], 26 elements // ['a' .. 'z'] = [0x61, 0x7a], 26 elements const char map[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char genid[9]; genid[8] = 0; PR_GetRandomNoise((void *) genid, 8); for (PRInt32 i = 0; i < 8; ++i) genid[i] = map[(genid[i] & 0x7F) % 52]; aString = genid; return NS_OK; }
nsresult nsUUIDGenerator::Init() { mLock = PR_NewLock(); NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY); // We're a service, so we're guaranteed that Init() is not going // to be reentered while we're inside Init(). #if !defined(XP_WIN) && !defined(XP_MACOSX) && !defined(ANDROID) /* initialize random number generator using NSPR random noise */ unsigned int seed; PRSize bytes = 0; while (bytes < sizeof(seed)) { PRSize nbytes = PR_GetRandomNoise(((unsigned char *)&seed)+bytes, sizeof(seed)-bytes); if (nbytes == 0) { return NS_ERROR_FAILURE; } bytes += nbytes; } /* Initialize a new RNG state, and immediately switch * back to the previous one -- we want to use mState * only for our own calls to random(). */ mSavedState = initstate(seed, mState, sizeof(mState)); setstate(mSavedState); mRBytes = 4; #ifdef RAND_MAX if ((unsigned long) RAND_MAX < (unsigned long)0xffffffff) mRBytes = 3; if ((unsigned long) RAND_MAX < (unsigned long)0x00ffffff) mRBytes = 2; if ((unsigned long) RAND_MAX < (unsigned long)0x0000ffff) mRBytes = 1; if ((unsigned long) RAND_MAX < (unsigned long)0x000000ff) return NS_ERROR_FAILURE; #endif #endif /* non XP_WIN and non XP_MACOSX */ return NS_OK; }
int main(int argc, char **argv) { { /* ** Get command line options */ PLOptStatus os; PLOptState *opt = PL_CreateOptState(argc, argv, "hdv"); while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { if (PL_OPT_BAD == os) continue; switch (opt->option) { case 'd': /* debug */ debug = 1; msgLevel = PR_LOG_ERROR; break; case 'v': /* verbose mode */ msgLevel = PR_LOG_DEBUG; break; case 'h': /* help message */ Help(); break; default: break; } } PL_DestroyOptState(opt); } lm = PR_NewLogModule("Test"); /* Initialize logging */ for ( i = 0; i < optRandCount ; i++ ) { memset( buf, 0, bufSize ); rSize = PR_GetRandomNoise( buf, bufSize ); if (!rSize) { fprintf(stderr, "Not implemented\n" ); failed_already = PR_TRUE; break; } if (debug) PrintRand( buf, rSize ); } if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS"); return( (failed_already == PR_TRUE )? 1 : 0 ); } /* main() */
NS_IMETHODIMP nsEnigMimeService::GetRandomHex(PRUint32 nDigits, char **_retval) { DEBUG_LOG(("nsEnigMimeService::GetRandomHex: %d\n", nDigits)); if (!_retval) return NS_ERROR_NULL_POINTER; if (nDigits < 1) return NS_ERROR_FAILURE; // Get random noise PRSize nBytes = (nDigits+1)/2; EMBool discardOneDigit = (nBytes*2 == nDigits+1); unsigned char *randomBuf = (unsigned char*) PR_Malloc(sizeof(char *) * nBytes ); PRSize randomBytes = PR_GetRandomNoise((void*)randomBuf, nBytes); if (randomBytes < nBytes) { PR_Free(randomBuf); return NS_ERROR_NOT_AVAILABLE; } // Convert random bytes to hexadecimal string nsCAutoString hex (""); for (PRUint32 j=0; j<nBytes; j++) { PRInt32 value = randomBuf[j]; if (discardOneDigit && (j == nBytes-1)) { value = value % 16; } else if (value < 16) { hex.Append("0"); } hex.AppendInt(value, 16); } PR_Free(randomBuf); *_retval = ToNewCString(hex); return NS_OK; }