Where can I find an industry-accepted secure pseudo-random number generator for Objective-C? Is there one built in to the OS X SDK?
My question is basically the same as this one, except that I'm looking for a secure PRNG.
EDIT:
Thanks everyone for the help. Here's a simple one-liner to implement the /dev/random method:
-(NSData *)getRandomBytes:(NSUInteger)length {
return [[NSFileHandle fileHandleForReadingAtPath:@"/dev/random"] readDataOfLength:length];
}
How Do I Generate a Random Number in Objective-C? tl;dr: Use arc4random() and its related functions. Specifically, to generate a random number between 0 and N - 1 , use arc4random_uniform() , which avoids modulo bias.
In the C programming language, the rand() function is a library function that generates the random number in the range [0, RAND_MAX]. When we use the rand() function in a program, we need to implement the stdlib. h header file because rand() function is defined in the stdlib header file.
Many SecureRandom implementations are in the form of a pseudo-random number generator (PRNG), which means they use a deterministic algorithm to produce a pseudo-random sequence from a true random seed. Other implementations may produce true random numbers, and yet others may use a combination of both techniques.
Security.framework has a facility for doing this, called SecRandomCopyBytes(). Although it's really basically just copying from /dev/random
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With