diff --git a/Classes/Utility/StringUtility.php b/Classes/Utility/StringUtility.php index 779a9d7..2d96d17 100644 --- a/Classes/Utility/StringUtility.php +++ b/Classes/Utility/StringUtility.php @@ -45,4 +45,23 @@ class StringUtility return $string; } + function getRandomString( + int $length = 64, + string $keyspace = null + ): string { + if (!$keyspace) { + $keyspace = '0123456789' + . 'abcdefghijklmnopqrstuvwxyz' + . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + } + if ($length < 1) { + throw new \RangeException("Length must be a positive integer"); + } + $pieces = []; + $max = mb_strlen($keyspace, '8bit') - 1; + for ($i = 0; $i < $length; ++$i) { + $pieces []= $keyspace[random_int(0, $max)]; + } + return implode('', $pieces); + } }