我已经从我在网上看到的各种代码中拼凑出这个算法(如果它可以被称为),我想知道它的密码安全性如何。它用于生成密码:
function seed_random() {
$seed = crc32(uniqid(sha1(microtime(true) . getmypid()), true));
mt_srand($seed);
$n = mt_rand(1, 200);
for ($i = 0; $i < $n; $i++) {
mt_rand();
}
}
function mt_rand_custom($min, $max) {
seed_random();
return mt_rand($min, $max);
}
function random_password($length) {
$legal_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'_!@#$^*?<>[]{}~(),";
seed_random();
$command = 'head -c 500 /dev/urandom | tr -dc %s | head -c %s; echo';
return shell_exec(sprintf($command, $legal_characters, $length));
}
function generate_password() {
return random_password(10);
}
我知道安全性,尤其是在 Web 环境中,取决于许多其他事情(例如通过 SSL 发送的密码),但我对这组特定的功能感到好奇。感谢您的反馈。
编辑:即使示例不再使用 mt_random_custom 函数,任何对此的反馈也将不胜感激,所以我把它留在了。