Skip to content

Commit

Permalink
Consistently be less redundant with if statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Oct 2, 2015
1 parent baa1465 commit 456d84c
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 251 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ weren't for the contributions of the following individuals:
* [@kelunik (Niklas Keller)](https://github.com/kelunik)
* [@lt (Leigh)](https://github.com/lt)
* [@MasonM (Mason Malone)](https://github.com/MasonM)
* [@mmeyer2k (Michael M)](https://mmeyer2k)

This comment has been minimized.

Copy link
@glensc

glensc Oct 15, 2015

Contributor

this link seems broken ;)

This comment has been minimized.

Copy link
@paragonie-scott

paragonie-scott Oct 15, 2015

Author Member

Yikes. How silly of me.

* [@narfbg (Andrey Andreev)](https://github.com/narfbg)
* [@oittaa](https://github.com/oittaa)
* [@redragonx (Stephen Chavez)](https://github.com/redragonx)
Expand Down
89 changes: 44 additions & 45 deletions lib/random_bytes_com_dotnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,51 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (!function_exists('random_bytes') && extension_loaded('com_dotnet')) {

/**
* Windows with PHP < 5.3.0 will not have the function
* openssl_random_pseudo_bytes() available, so let's use
* CAPICOM to work around this deficiency.
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError(
'Length must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
$buf = '';
$util = new COM('CAPICOM.Utilities.1');
$execCount = 0;
/**
* Windows with PHP < 5.3.0 will not have the function
* openssl_random_pseudo_bytes() available, so let's use
* CAPICOM to work around this deficiency.
*
* @param int $bytes
*
* @throws Exception
*
* @return string
* Let's not let it loop forever. If we run N times and fail to
* get N bytes of random data, then CAPICOM has failed us.
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError(
'Length must be an integer'
);
do {
$buf .= base64_decode($util->GetRandom($bytes, 0));
if (RandomCompat_strlen($buf) >= $bytes) {
/**
* Return our random entropy buffer here:
*/
return RandomCompat_substr($buf, 0, $bytes);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
$buf = '';
$util = new COM('CAPICOM.Utilities.1');
$execCount = 0;
/**
* Let's not let it loop forever. If we run N times and fail to
* get N bytes of random data, then CAPICOM has failed us.
*/
do {
$buf .= base64_decode($util->GetRandom($bytes, 0));
if (RandomCompat_strlen($buf) >= $bytes) {
/**
* Return our random entropy buffer here:
*/
return RandomCompat_substr($buf, 0, $bytes);
}
++$execCount;
} while ($execCount < $bytes);
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'PHP failed to generate random data.'
);
}
++$execCount;
} while ($execCount < $bytes);
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'PHP failed to generate random data.'
);
}
79 changes: 39 additions & 40 deletions lib/random_bytes_mcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,45 @@
* SOFTWARE.
*/

if (!function_exists('random_bytes') && function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) {
/**
* Powered by ext/mcrypt (and thankfully NOT libmcrypt)
*
* @ref https://bugs.php.net/bug.php?id=55169
* @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError(
'Length must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}

$buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
if ($buf !== false) {
if (RandomCompat_strlen($buf) === $bytes) {
/**
* Return our random entropy buffer here:
*/
return $buf;
}
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'PHP failed to generate random data.'

/**
* Powered by ext/mcrypt (and thankfully NOT libmcrypt)
*
* @ref https://bugs.php.net/bug.php?id=55169
* @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError(
'Length must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}

$buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
if ($buf !== false) {
if (RandomCompat_strlen($buf) === $bytes) {
/**
* Return our random entropy buffer here:
*/
return $buf;
}
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'PHP failed to generate random data.'
);
}
82 changes: 40 additions & 42 deletions lib/random_bytes_openssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,49 @@
* SOFTWARE.
*/

if (!function_exists('random_bytes') && function_exists('openssl_random_pseudo_bytes')) {
/**
* Since openssl_random_pseudo_bytes() uses openssl's
* RAND_pseudo_bytes() API, which has been marked as deprecated by the
* OpenSSL team, this is our last resort before failure.
*
* @ref https://www.openssl.org/docs/crypto/RAND_bytes.html
*
* @param int $bytes
*
* @throws Exception
*
* @return string
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError(
'Length must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
}
$secure = true;
/**
* Since openssl_random_pseudo_bytes() uses openssl's
* RAND_pseudo_bytes() API, which has been marked as deprecated by the
* OpenSSL team, this is our last resort before failure.
*
* @ref https://www.openssl.org/docs/crypto/RAND_bytes.html
*
* @param int $bytes
* $secure is passed by reference. If it's set to false, fail. Note
* that this will only return false if this function fails to return
* any data.
*
* @throws Exception
*
* @return string
* @ref https://github.com/paragonie/random_compat/issues/6#issuecomment-119564973
*/
function random_bytes($bytes)
{
if (!is_int($bytes)) {
throw new TypeError(
'Length must be an integer'
);
}
if ($bytes < 1) {
throw new Error(
'Length must be greater than 0'
);
$buf = openssl_random_pseudo_bytes($bytes, $secure);
if ($buf !== false && $secure) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
$secure = true;
/**
* $secure is passed by reference. If it's set to false, fail. Note
* that this will only return false if this function fails to return
* any data.
*
* @ref https://github.com/paragonie/random_compat/issues/6#issuecomment-119564973
*/
$buf = openssl_random_pseudo_bytes($bytes, $secure);
if ($buf !== false && $secure) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'PHP failed to generate random data.'
);
}
/**
* If we reach here, PHP has failed us.
*/
throw new Exception(
'PHP failed to generate random data.'
);
}
Loading

0 comments on commit 456d84c

Please sign in to comment.