Browse Source

Security::generateRandomKey enhancements:

- Equals sign is now replaced with dot.
- Slash is now replaced with dash.
- Better phpdoc.
tags/2.0.0-beta
Alexander Makarov 11 years ago
parent
commit
a8d21805f5
  1. 6
      framework/yii/helpers/SecurityBase.php

6
framework/yii/helpers/SecurityBase.php

@ -133,19 +133,19 @@ class SecurityBase
} }
/** /**
* Generates a random key. * Generates a random key. The key may contain uppercase and lowercase latin letters, digits, underscore, dash and dot.
* @param integer $length the length of the key that should be generated * @param integer $length the length of the key that should be generated
* @return string the generated random key * @return string the generated random key
*/ */
public static function generateRandomKey($length = 32) public static function generateRandomKey($length = 32)
{ {
if (function_exists('openssl_random_pseudo_bytes')) { if (function_exists('openssl_random_pseudo_bytes')) {
$key = strtr(base64_encode(openssl_random_pseudo_bytes($length, $strong)), array('+' => '_', '/' => '~')); $key = strtr(base64_encode(openssl_random_pseudo_bytes($length, $strong)), array('+' => '_', '/' => '-', '=' => '.'));
if ($strong) { if ($strong) {
return substr($key, 0, $length); return substr($key, 0, $length);
} }
} }
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~'; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.';
return substr(str_shuffle(str_repeat($chars, 5)), 0, $length); return substr(str_shuffle(str_repeat($chars, 5)), 0, $length);
} }

Loading…
Cancel
Save