我目前正在修改我的注册脚本以添加 PDO 和 bcrypt。但是,当我尝试迭代我的哈希时,我遇到了一个错误。我最初将轮数设置为 10000,因为我看到了 60000+ 轮的教程,但这需要很长时间。所以我把它设置为 2 只是为了测试它,然后我得到一个错误:
[Tue Dec 25 10:45:07 2012] [error] [] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133431193 bytes) in /var/www/register_script.php on line 28, referer:
我的整个注册脚本都是休闲的:
<?php
//Minor work needed need to finish user verification
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="testdbpass"; // Mysql password
$db_name="test"; // Database name
// Connect to server via PHP Data Object
$dbh = new PDO("mysql:host=localhost;dbname=test;", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
CRYPT_BLOWFISH or die ('No Blowfish found.');
// Creating the salt
$Blowfish_Pre = '$2y$15$';
$Blowfish_End = '$';
$Allowed_Chars =
'/.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$char_len = 63;
$salt_length = 60;
for($round=0;$round<$salt_length;$roundi++)
{
$salt .= $Allowed_Chars[mt_rand(0,$char_len)];
}
$bcrypt_salt = $Blowfish_Pre . $salt . $Blowfish_End;
//Salt creating stops here
//Creating the hash and password
$password = $_POST['password'];
$hashed_password = crypt($password, $bcrypt_salt);
for($round=0; $round<2; $round++)
{
$hashed_password = crypt($password, $bcrypt_salt);
}
// Insert statements with PDO
try {
$query = $dbh->prepare("INSERT INTO `users_blowfish` (username, email, fname, lname, salt, password)
VALUES (:username, :email, :first, :last, :salt, :hash)");
$query->execute(
array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'first' => $_POST['fname'],
'last' => $_POST['lname'],
'salt' => $bcrypt_salt,
'hash' => $hashed_password
));
}
catch (PDOException $e) {
error_log($e->getMessage());
die($e->getMessage());
}
$dbh= null;
?>
<html>
<body>
<p>
Thank you for registering your account. Please wait for administrator approval before doing anything else. Thank you - System Administrator.
</p>
</body>
</html>
如果我取出 for 语句:
$hashed_password = crypt($password, $bcrypt_salt);
for($round=0; $round<2; $round++)
{
$hashed_password = crypt($password, $bcrypt_salt);
}
然后一切正常。然而让我感到困惑的是,我有两个 for 语句,上面的一个 ^
和这个 :
$salt_length = 60;
for($round=0;$round<$salt_length;$roundi++)
{
$salt .= $Allowed_Chars[mt_rand(0,$char_len)];
}
我想我总结的问题是 1)为什么哈希的 for 语句使注册非常慢,而盐创建的 for 语句不影响注册速度?