Webiny\Component\Crypt\Crypt::generateRandomString PHP Method

generateRandomString() public method

If $chars param is empty, the string will be generated using numbers, letters and special characters.
public generateRandomString ( integer $length, string $chars = '' ) : string
$length integer Length of the generated string.
$chars string A string containing a list of chars that will be uses for generating the random string.
return string Random string with the given $length containing only the provided set of $chars.
    public function generateRandomString($length, $chars = '')
    {
        try {
            return $this->driverInstance->generateRandomString($length, $chars);
        } catch (\Exception $e) {
            throw new CryptException($e->getMessage());
        }
    }

Usage Example

Example #1
0
 public function testGenerateRandomString()
 {
     $crypt = new Crypt();
     $randomString = $crypt->generateRandomString(9, 'abc');
     $this->assertInternalType('string', $randomString);
 }