Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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