Skip to content

Instantly share code, notes, and snippets.

@scottchiefbaker
Created March 16, 2015 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottchiefbaker/d191f369765eef5ed0cf to your computer and use it in GitHub Desktop.
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
<?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