From 64807ba686860bbb9447d31bb0c7c7d705969939 Mon Sep 17 00:00:00 2001 From: Nikita Sinelnikov Date: Sat, 24 Nov 2018 17:56:47 +0200 Subject: [PATCH] added alternative random_bytes() added alternative random_bytes() instead deprecated in PHP 7.2 mcrypt_create_iv() --- .../McryptPseudoRandomStringGenerator.php | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Facebook/PseudoRandomString/McryptPseudoRandomStringGenerator.php b/src/Facebook/PseudoRandomString/McryptPseudoRandomStringGenerator.php index bf5737453..228af9843 100644 --- a/src/Facebook/PseudoRandomString/McryptPseudoRandomStringGenerator.php +++ b/src/Facebook/PseudoRandomString/McryptPseudoRandomStringGenerator.php @@ -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.' + ); + } } } @@ -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.' ); }