ircmaxell/RandomLib

Compatibility with multibyte characters

Trainmaster opened this issue · 1 comments

$factory = new RandomLib\Factory;
$generator = $factory->getGenerator(new SecurityLib\Strength(SecurityLib\Strength::MEDIUM));

var_dump($generator->generateString(8, 'abc'));
// string(8) "bbcaaacb"

var_dump($generator->generateString(8, 'äöü'));
// string(8) "ö¼Ã¤Ã¼¶"

var_dump($generator->generateString(8, 'a'));
// string(0) ""

var_dump($generator->generateString(8, 'ä'));
// string(8) "¤¤Ã¤¤¤Ã¤"

In general, this will not be solvable unless significant constraints are put on the character set. As in make that a 3rd option to the method. This would require abstracting mb_string and intl, as we're not sure if they are available, and which one is.

Which means it's definitely possible, but it's not trivial. I'd suggest in the mean time doing it yourself:

$string = "";
$chars = "äöü";
$len = mb_strlen($chars, "utf-8");
for ($i = 0; $i < 8; $i++) {
    $string .= $chars[$generator->generateInt(0, $len)];
}