M H*_*M H 4 php security random cryptography
目标:找到最加密的安全随机字符串生成器.在字符串中使用字母,数字和可能的特殊字符.
我一直在这里和其他地方读书,但我仍然听到很多不同的答案/意见.那些对安全性和加密技术有最新知识且知识渊博的人可以在这里发出声音.
以下函数将用于生成8个字符的随机密码,并生成128个字符的随机令牌.
功能1:
/**
* Used for generating a random string.
*
* @param int $_Length The lengtyh of the random string.
* @return string The random string.
*/
function gfRandomString($_Length) {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < $_Length; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
Run Code Online (Sandbox Code Playgroud)
功能2:
PHP.net文档说: crypto_strong: 如果传入函数,这将保存一个布尔值,确定所使用的算法是否"加密强",例如,使用GPG,密码等是否安全.如果确实如此,则为TRUE,否则为FALSE.
那么基于服务器呢?如果我曾经测试它,它是能够产生crypto_strong字符串,将它随时都可以?或者我是否需要每次检查并创建一个循环,直到它生成一个crypto_strong字符串.
/**
* Used for generating a random string.
*
* @param int $_Length The length of bits.
* @return string The random string.
*/
function gfSecureString($_Length) {
$Str = bin2hex(openssl_random_pseudo_bytes($_Length));
return $Str;
}
Run Code Online (Sandbox Code Playgroud)
我欢迎任何提高加密强度的建议.
因此,您希望在PHP中安全地生成随机字符串.问题中的两个函数都没有给你你想要的东西,但rand()
解决方案是两者中最差的.rand()
不安全,同时bin2hex(openssl_random_pseudo_bytes())
限制输出字符集.
此外,在极端条件或异国情调的设置下openssl_random_pseudo_bytes()
可能不可靠.
从我的理解,crypto_strong
将只设定为false
如果RAND_pseudo_bytes()
不返回任何数据.如果OpenSSL在调用时没有播种,它将静默地返回弱(并且可能是可预测的)伪随机字节.你无法从PHP中确定它是否是随机的.
如果您想要一个已经获得PHP 5.x实质性审核的解决方案,请使用RandomLib.
$factory = new RandomLib\Factory;
$generator = $factory->getMediumStrengthGenerator();
$randomPassword = $generator->generateString(20, $alphabet);
Run Code Online (Sandbox Code Playgroud)
如果您不想使用RandomLib(即使纯粹因为您希望有其他选项可用),您也可以random_int()
在PHP 7出现时使用.如果您不能等到那时,请查看我们的random_compat项目.
如果您正在使用加密库libsodium,您可以生成如下随机数:
/**
* Depends on the PECL extension libsodium
*
* @link https://stackoverflow.com/a/31498051/2224584
*
* @param int $length How long should the string be?
* @param string $alphabet Contains all of the allowed characters
*
* @return string
*/
function sodium_random_str($length, $alphabet = 'abcdefghijklmnopqrstuvwxyz')
{
$buf = '';
$alphabetSize = strlen($alphabet);
for ($i = 0; $i < $length; ++$i) {
$buf .= $alphabet[\Sodium\randombytes_uniform($alphabetSize)];
}
return $buf;
}
Run Code Online (Sandbox Code Playgroud)
有关使用的示例代码,请参阅此答案random_int()
.如果需要,我宁愿不重复将来更新代码的工作.