【发布时间】:2014-12-31 11:15:41
【问题描述】:
我找到了一个解决下面提到的问题的方法,它可以帮助人们使用 PHP PDO。我对其进行了测试并且它可以工作,但我不确定它是最干净的代码还是最好的。欢迎任何改进。
这里是原始问题供参考:
我想对 MySQL 数据库中已有的密码进行哈希处理。我已经可以使用 php 5.5 散列 API 散列新密码,但我想知道是否有办法获取所有旧的纯文本密码并将它们转换为 bcrypt 散列。我现在正在考虑将密码复制到一个名为“散列”的新行,并在检查它们是否正确复制后,将它们转换为散列。不过,我不确定如何复制密码行并将其重命名在同一张表上,或者如何最有效地对所有这些进行哈希处理。
任何见解将不胜感激。
解决方法如下:
<?
// IMPORTANT: only call this script one time or you will double hash and the passwords input by your users won't work anymore
// Get Configuration file
require("configsecuresavedgames.php");
// Connect to your server
$dbh = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8" , $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
///////////////////////////////////////////////////////
// Upload new score
///////////////////////////////////////////////////////
// set variable $x to 1 to start at ID 1 and then update each row in a loop, adding 1 to the $x variable once done
$x = 1;
// Note: Change the statement below so that the number is larger to match the number of users in your database
while($x <= 100) {
// select hash for each row...
$stmt = $dbh->prepare("SELECT hash FROM $tname WHERE id = $x");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
// set $hash variable to hash (from database) for the respective row
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['hash'];
$hash = $row ['hash'];
}
// update hash row with new hash data (note: prior to running the script make sure that you've copied all plain text passwords to the hash row in the database.
$newhash = password_hash($hash, PASSWORD_DEFAULT);
$sql = "UPDATE securesavegames SET hash = '$newhash' WHERE id = $x";
// Prepare statement
$stm = $dbh->prepare($sql);
// execute the query
$stm->execute();
// echo a message to say the UPDATE succeeded
echo $stm->rowCount() . " records UPDATED successfully";
// add to $x so that the hash for the next 'id' will be updated, then the loop will continue.
$x++;
}
$dbh = null;
?>
【问题讨论】:
-
没有。哈希=绞肉机。哈希将一头牛变成汉堡包。没有办法把汉堡包拿回去变回原来的牛。您必须让您的用户重新输入他们的密码,以便他们可以通过新的哈希值运行。
-
@MarcB 我认为他所指的密码已经是纯文本。
-
啊,好吧……在那种情况下
update yourtable set new_hash = hash(old_password_field)。你可以做一个就地set pw=hash(pw),但是你会丢失原始的明文。 -
嗨 MarcB,我可以在 phpmyadmin 中运行上述 SQL 查询并在新的“哈希”行中获取我的密码的哈希值吗?这就是我基本上想做的事情。
标签: php mysql sql hash passwords