Created
March 16, 2015 22:32
-
-
Save scottchiefbaker/d191f369765eef5ed0cf to your computer and use it in GitHub Desktop.
Initial work on a userland implementation of the Easy User-land CSPRNG RFC
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
<?php | |
if (!function_exists("random_bytes")) { | |
function random_bytes($bytes = 16) | |
{ | |
$ret = ''; | |
if (function_exists("mcrypt_create_ivz")) { | |
$ret = mcrypt_create_iv($bytes); | |
} elseif (function_exists("openssl_random_pseudo_bytesz")) { | |
$ret = openssl_random_pseudo_bytes($bytes); | |
} elseif (is_readable("/dev/urandom")) { | |
$fh = fopen("/dev/urandom",'r'); | |
$ret = fread($fh,$bytes); | |
fclose($fh); | |
} else { | |
throw new Exception('No valid crypto source found'); | |
} | |
return $ret; | |
} | |
function random_int() | |
{ | |
$bytes = random_bytes(8); | |
$int = unpack('q',$bytes); | |
return $int[1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment