Skip to content

Commit

Permalink
Demote openssl_random_pseudo_bytes. Closes #5 (thanks @MasonM)
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Jul 8, 2015
1 parent f146da1 commit f9c275f
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/random.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ function_exists('mcrypt_create_iv') &&
}
}
}

if (function_exists('openssl_random_pseudo_bytes')) {
$secure = true;
$buf = openssl_random_pseudo_bytes($bytes, $secure);
if ($buf !== false && $secure) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
}
}

/**
* Use /dev/urandom for random numbers
Expand Down Expand Up @@ -96,6 +86,24 @@ function_exists('mcrypt_create_iv') &&
unset($e); // Let's not let CAPICOM errors kill our app
}
}

/**
* 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
*/
if (function_exists('openssl_random_pseudo_bytes')) {
$secure = true;
$buf = openssl_random_pseudo_bytes($bytes, $secure);
if ($buf !== false && $secure) {
if (RandomCompat_strlen($buf) === $bytes) {
return $buf;
}
}
}

/**
* We have reached the point of no return. Throw an exception.
*/
Expand Down

0 comments on commit f9c275f

Please sign in to comment.