Skip to content

Commit

Permalink
use random_bytes if available (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
oittaa authored and bshaffer committed Nov 15, 2017
1 parent c2d2735 commit fdcf253
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/OAuth2/ResponseType/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ public function createAccessToken($client_id, $user_id, $scope = null, $includeR
*/
protected function generateAccessToken()
{
if (function_exists('random_bytes')) {
$randomData = random_bytes(20);
if ($randomData !== false && strlen($randomData) === 20) {
return bin2hex($randomData);
}
}
if (function_exists('openssl_random_pseudo_bytes')) {
$randomData = openssl_random_pseudo_bytes(20);
if ($randomData !== false && strlen($randomData) === 20) {
Expand Down Expand Up @@ -209,4 +215,4 @@ public function revokeToken($token, $tokenTypeHint = null)

return $revoked;
}
}
}
4 changes: 3 additions & 1 deletion src/OAuth2/ResponseType/AuthorizationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function enforceRedirect()
protected function generateAuthorizationCode()
{
$tokenLen = 40;
if (function_exists('openssl_random_pseudo_bytes')) {
if (function_exists('random_bytes')) {
$randomData = random_bytes(100);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$randomData = openssl_random_pseudo_bytes(100);
} elseif (function_exists('mcrypt_create_iv')) {
$randomData = mcrypt_create_iv(100, MCRYPT_DEV_URANDOM);
Expand Down

0 comments on commit fdcf253

Please sign in to comment.