Created
December 7, 2011 18:11
-
-
Save mmower/1443904 to your computer and use it in GitHub Desktop.
Implementation of SecRandomCopyBytes for OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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