Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Random Numbers Securely in Objective-C

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];
}
like image 590
pepsi Avatar asked Feb 10 '12 20:02

pepsi


People also ask

How do you generate a random number in Objective C?

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.

Can you generate random numbers in C?

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.

Is SecureRandom truly random?

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.


1 Answers

Security.framework has a facility for doing this, called SecRandomCopyBytes(). Although it's really basically just copying from /dev/random.

like image 101
Lily Ballard Avatar answered Oct 27 '22 21:10

Lily Ballard