Beispiel #1
0
 /**
  * Generator of secure random tokens.
  *
  * Note that it is best to use an even number for the length.
  *
  * @param int|null $length (defaults to defaultLength)
  * @return string Key
  */
 public function generateKey($length = null)
 {
     if (empty($length)) {
         $length = $this->defaultLength;
     }
     if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
         $function = 'random_bytes';
     } elseif (extension_loaded('openssl')) {
         $function = 'openssl_random_pseudo_bytes';
     } else {
         trigger_error('Not secure', E_USER_DEPRECATED);
         return Random::pwd($length);
     }
     $value = bin2hex($function($length / 2));
     if (strlen($value) !== $length) {
         $value = str_pad($value, $length, '0');
     }
     return $value;
 }
 public function testPwd()
 {
     $result = Random::pwd(10);
     $this->assertTrue(mb_strlen($result) === 10);
 }
 /**
  * Generator
  *
  * @param length (defaults to defaultLength)
  * @return string Key
  */
 public function generateKey($length = null)
 {
     if (empty($length)) {
         $length = $this->defaultLength;
     }
     return Random::pwd($length);
 }