Partial output of random numbers string
Opened this issue · 2 comments
First of all I'd like to disclose that I'm neither professional nor hobbyist programmer/coder. Just an aspiring end user :)
I have the following code:
<?php
$factory = new RandomLib\Factory;
$generator = $factory->getMediumStrengthGenerator();
$genoutext = $generator->generateString(64, '!"#$%&\'\\(\\)\*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~');
?>
<?php echo $genoutext; ?>
<br />
<?php echo mb_strlen($genoutext); ?>
The issue is that, sometimes, the output random string (echo $genoutext;) will have fewer than 64 characters in it, I've seen as low as 2, and yet at all times mb_strlen output will show the number 64.
I suppose, perhaps, I may not really understand how mb_strlen is supposed to work, but what could be the cause of the generator outputting less random data then it is specified? Just a guess, could it be that sometimes not enough entropy is available on my shared hosting account? In case if that might be so, is there a way to delay the output of random data until enough entropy is accessible to generate the full 64 character random string?
Thank you!
That shouldn't be possible... It should literally always return 64 characters, or infinite loop trying to (specifically, this loop: https://github.com/ircmaxell/RandomLib/blob/master/lib/RandomLib/Generator.php#L165 )
Hang on, when you say "echo", do you mean in the middle of a webpage? Because the presence of <
or "
characters may wind up hiding part of the output into the source of the page. Or perhaps your template engine is stripping them. Or something of that fact. If mb_strlen (or strlen) reports 64 characters, it's being correctly generated (indeed, otherwise it would have to infinitely loop).
I would look elsewhere in your stack to see if something else is messing with the string prior to output (or if it's just being hidden by the browser)...
Yes, I'm trying to output random string on a webpage.
Indeed, I have escaped some special characters, but other characters have been successfully forgotten by me.
Thank you so much for pointing this out, and thank you for your excellent software creations!