Skip to content

added alternative random_bytes() #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ class McryptPseudoRandomStringGenerator implements PseudoRandomStringGeneratorIn
*/
public function __construct()
{
if (!function_exists('mcrypt_create_iv')) {
throw new FacebookSDKException(
static::ERROR_MESSAGE .
'The function mcrypt_create_iv() does not exist.'
);
if (version_compare( PHP_VERSION, '7.1', '>=' )) {
if (!function_exists('random_bytes')) {
throw new FacebookSDKException(
static::ERROR_MESSAGE .
'The function random_bytes() does not exist.'
);
}
} else {
if (!function_exists('mcrypt_create_iv')) {
throw new FacebookSDKException(
static::ERROR_MESSAGE .
'The function mcrypt_create_iv() does not exist.'
);
}
}
}

Expand All @@ -54,12 +63,16 @@ public function getPseudoRandomString($length)
{
$this->validateLength($length);

$binaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
if (version_compare( PHP_VERSION, '7.1', '>=' )) {
$binaryString = random_bytes($length);
} else {
$binaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
}

if ($binaryString === false) {
throw new FacebookSDKException(
static::ERROR_MESSAGE .
'mcrypt_create_iv() returned an error.'
'mcrypt_create_iv() or random_bytes() returned an error.'
);
}

Expand Down