CFHipsterRef Low-Level Programming on iOS & Mac OS X

Randomization Services

Cryptography is predicated on unpredictable, random values. Without such a guarantee, it’s all just security theater. SecRandomCopyBytes reads from /dev/random, which generates cryptographically-secure random bytes. /dev/random is a special file on Unix systems that streams entropy based on the environmental noise of the device.

NSUInteger length = 1024;

NSMutableData *mutableData = [NSMutableData dataWithLength:length];

OSStatus success = SecRandomCopyBytes(kSecRandomDefault,
length, mutableData.mutableBytes);

__Require_noErr(success, exit);