When it comes to encryption/security issues, the ideal is to use tools made even for this.
An example in PHP is openssl_random_pseudo_bytes, a function that was made to generate random numbers (with a configurable number of bytes).
$bytes = openssl_random_pseudo_bytes($length, $strong);
if (!$strong) {
//Tratar o erro
exit();
}
$hex = bin2hex($bytes);
This library is not always available, which can be problematic.
On Linux, the /dev/random is a special "file" that allows you to extract random bytes.
The /dev/urandom is very similar to /dev/random, although they have slightly different behaviors when the computer is "without entropy", that is, it has no more data to search for as a source of random numbers.
Supposedly, as far as is known, use this file for security purposes.
$file = fopen('/dev/urandom', 'r');
if (!$file) {
// Falhou
exit();
}
$bytes = fread($file, $length);
fclose($file);
$hex = bin2hex($bytes);
This second solution has the disadvantage of not being available on Windows.
What doesn’t fit
rand and mt_rand They weren’t designed for cryptography, so they don’t fit.
The function mt_rand is described by the documentation as "better" than the rand, but still has this warning:
Caution
This Function does not generate cryptographically Secure values, and
should not be used for Cryptographic purposes. If you need a
cryptographically Secure value, consider using
openssl_random_pseudo_bytes() Instead.
In English:
Care
This function does not generate cryptographically secure values, and should not
be used for cryptographic purposes. If you need a value
cryptographically secure, consider using openssl_random_pseudo_bytes in
time of this.
The function uniqid also does not serve (see the notice in the documentation itself, the notice is not available in all languages):
This Function does not create Random nor unpredictable string. This
Function must not be used for security purposes. Use cryptographically
Secure Random Function/Generator and cryptographically Secure hash
functions to create unpredictable Secure ID.
In Portuguese:
This function does not create random or unpredictable strings. This function
cannot be used for security purposes. Use number generators
safe random and cryptographically secure hash functions for
create secure unpredictable Ids.
The hash(md5, sha, etc.) of a value that is not random enough for cryptographic purposes is still not cryptographically secure.
maybe you should put the code of this routine in your answer, it is easier for people to see and if one day the link fails your answer will still be valid.
– Math
I agree with you that you could have set an example, but you didn’t have time to write and test a routine, which could become outdated with time and updates in the PHP API. The link is directly to the PHP API page, from where it is not allowed to reproduce the content of the site, so you could not copy and paste the example here. But I thank you for your suggestions and I will take it into consideration for my next responses.
– Vitor Arbex
@Math There is another license problem, all content placed on the site is automatically licensed by cc by-sa 3.0, unless the code was made by me, it could only post some ready code on the web if the license was compatible. What in the case of the PHP site is not, because the content of their site is Copyright and is not allowed to copy all or part of it. Therefore, care is needed when posting third party code here.
– Vitor Arbex