Created
May 30, 2012 20:25
-
-
Save ezimuel/2838705 to your computer and use it in GitHub Desktop.
Zend\Math\Math::randBytes fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static function randBytes($length, $strong = false) | |
{ | |
if ($length <= 0) { | |
return false; | |
} | |
if (extension_loaded('openssl')) { | |
$rand = openssl_random_pseudo_bytes($length, $secure); | |
if ($secure === true) { | |
return $rand; | |
} | |
} | |
if (extension_loaded('mcrypt')) { | |
$rand = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); | |
if ($rand !== false && strlen($rand) === $length) { | |
return $rand; | |
} | |
} | |
if ($strong) { | |
throw new Exception\RuntimeException( | |
'This PHP environment doesn\'t support secure random number generation. ' . | |
'Please consider to install the OpenSSL and/or Mcrypt extensions' | |
); | |
} | |
$rand = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$rand .= chr(mt_rand(0, 255)); | |
} | |
return $rand; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment