Skip to content

Instantly share code, notes, and snippets.

@mmower
Created December 7, 2011 18:11
Show Gist options
  • Save mmower/1443904 to your computer and use it in GitHub Desktop.
Save mmower/1443904 to your computer and use it in GitHub Desktop.
Implementation of SecRandomCopyBytes for OS X
#ifdef TARGET_OS_MAC
typedef const struct __SecRandom * SecRandomRef;
const SecRandomRef kSecRandomDefault = NULL;
int SecRandomCopyBytes( SecRandomRef rnd, size_t count, uint8_t *bytes );
int SecRandomCopyBytes( SecRandomRef rnd, size_t count, uint8_t *bytes ) {
int fd;
if( ( fd = open("/dev/urandom", O_RDONLY) ) < 0 ) {
return -1;
}
ssize_t bytesRead;
uint8_t *p = bytes;
do {
bytesRead = read( fd, p, count - ( p -bytes ) );
if( bytesRead > 0 ) {
p += bytesRead;
}
} while( bytesRead > 0 || ( bytesRead < 0 && errno == EINTR ) );
close( fd );
return bytesRead < 0 ? -1 : 0;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment