Skip to content

Commit ffb4632

Browse files
oittaabshaffer
authored andcommitted
use random_bytes if available (bshaffer#834)
1 parent 1936fa9 commit ffb4632

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/OAuth2/ResponseType/AccessToken.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ public function createAccessToken($client_id, $user_id, $scope = null, $includeR
132132
*/
133133
protected function generateAccessToken()
134134
{
135+
if (function_exists('random_bytes')) {
136+
$randomData = random_bytes(20);
137+
if ($randomData !== false && strlen($randomData) === 20) {
138+
return bin2hex($randomData);
139+
}
140+
}
135141
if (function_exists('openssl_random_pseudo_bytes')) {
136142
$randomData = openssl_random_pseudo_bytes(20);
137143
if ($randomData !== false && strlen($randomData) === 20) {
@@ -209,4 +215,4 @@ public function revokeToken($token, $tokenTypeHint = null)
209215

210216
return $revoked;
211217
}
212-
}
218+
}

src/OAuth2/ResponseType/AuthorizationCode.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function enforceRedirect()
8484
protected function generateAuthorizationCode()
8585
{
8686
$tokenLen = 40;
87-
if (function_exists('openssl_random_pseudo_bytes')) {
87+
if (function_exists('random_bytes')) {
88+
$randomData = random_bytes(100);
89+
} elseif (function_exists('openssl_random_pseudo_bytes')) {
8890
$randomData = openssl_random_pseudo_bytes(100);
8991
} elseif (function_exists('mcrypt_create_iv')) {
9092
$randomData = mcrypt_create_iv(100, MCRYPT_DEV_URANDOM);

0 commit comments

Comments
 (0)