4
4
* for using the new PHP 7 random_* API in PHP 5 projects
6
6
* The MIT License (MIT)
8
* Copyright (c) 2015 Paragon Initiative Enterprises
8
* Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
10
10
* Permission is hereby granted, free of charge, to any person obtaining a copy
11
11
* of this software and associated documentation files (the "Software"), to deal
30
if ( ! is_callable( 'random_bytes' ) ):
32
* Powered by ext/mcrypt (and thankfully NOT libmcrypt)
34
* @ref https://bugs.php.net/bug.php?id=55169
35
* @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
43
function random_bytes($bytes)
46
$bytes = RandomCompat_intval($bytes);
47
} catch (TypeError $ex) {
49
'random_bytes(): $bytes must be an integer'
55
'Length must be greater than 0'
59
$buf = @mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
63
RandomCompat_strlen($buf) === $bytes
29
if (!is_callable('random_bytes')) {
31
* Powered by ext/mcrypt (and thankfully NOT libmcrypt)
33
* @ref https://bugs.php.net/bug.php?id=55169
34
* @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
42
function random_bytes($bytes)
45
$bytes = RandomCompat_intval($bytes);
46
} catch (TypeError $ex) {
48
'random_bytes(): $bytes must be an integer'
54
'Length must be greater than 0'
58
$buf = @mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
62
RandomCompat_strlen($buf) === $bytes
65
* Return our random entropy buffer here:
66
* Return our random entropy buffer here:
71
* If we reach here, PHP has failed us.
74
'Could not gather sufficient random data'
72
* If we reach here, PHP has failed us.
75
'Could not gather sufficient random data'