Neos\Flow\Utility\Algorithms::generateRandomString PHP Méthode

generateRandomString() public static méthode

Returns a random string with alpha-numeric characters.
public static generateRandomString ( integer $count, string $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ) : string
$count integer Number of characters to generate
$characters string Allowed characters, defaults to alpha-numeric (a-zA-Z0-9)
Résultat string A random string
    public static function generateRandomString($count, $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    {
        $characterCount = \Neos\Utility\Unicode\Functions::strlen($characters);
        $string = '';
        for ($i = 0; $i < $count; $i++) {
            $string .= \Neos\Utility\Unicode\Functions::substr($characters, random_int(0, $characterCount - 1), 1);
        }
        return $string;
    }

Usage Example

 /**
  * Generates a new key & saves it encrypted with a hashing strategy
  *
  * @param string $name
  * @return string
  * @throws SecurityException
  */
 public function generateKey($name)
 {
     if (strlen($name) === 0) {
         throw new SecurityException('Required name argument was empty', 1334215474);
     }
     $password = UtilityAlgorithms::generateRandomString($this->passwordGenerationLength);
     $this->persistKey($name, $password);
     return $password;
 }
All Usage Examples Of Neos\Flow\Utility\Algorithms::generateRandomString