<分区>
使用 Php 和 MySQL,我想更新数据库中特定列的所有行,
例如。 : 在“姓名”列中有一行包含“我的姓名”,
这是我试图实现的方案和逻辑:
循环数据库
获取每一行的当前值并用 hash('...', value); 散列它;
获取每一行的现有值并对其进行哈希处理并更新,这就是我想要更新完整数据库的方式,
我怎样才能做到这一点?
<分区>
使用 Php 和 MySQL,我想更新数据库中特定列的所有行,
例如。 : 在“姓名”列中有一行包含“我的姓名”,
这是我试图实现的方案和逻辑:
循环数据库
获取每一行的当前值并用 hash('...', value); 散列它;
获取每一行的现有值并对其进行哈希处理并更新,这就是我想要更新完整数据库的方式,
我怎样才能做到这一点?
最佳答案
首先我必须说,如果你在数据库中有非敏感数据,那么内置的 mysql 函数可以直接使用 mysql 更新语句为你提供哈希结果。
这个答案与此无关。它与敏感数据有关,例如密码。
我给了你一个指向 PHP password_hash()
和 password_verify()
示例的链接。
这里是 That Link再次。左侧的链接用于 PDO。以下Link Right Here和mysqli类似。
在 PDO 链接中查看行
$hp=password_hash($ctPassword,PASSWORD_DEFAULT); // hashed password, using
假设您现在有一个名为 ctPassword
的明文列。您可以更改表
并为hashedPassword
之类的内容添加一个新列。按照我提供的链接进行相应的调整,使用更新语句将 ctPassword
的值散列到 hashedPassword
中。
然后彻底测试它。当世界上一切正常时,删除 ctPassword
列并且不再使用它。 明确,切勿将明文密码存储在数据库中。存储单向哈希值,并根据它们进行验证。上面的链接显示了如何。
这里完全来自 PHP,我认为这需要从中驱动,而不是 mysql 哈希函数,糟糕。毕竟,您使用的是 PHP,它们强大的散列和验证功能将大放异彩。我认为最佳实践,而 mysql 人员并没有完全将精力花在它上面。我完全支持在 mysql 中做尽可能多的事情。但从来没有这个话题,使用哈希。让 PHP 驱动这个。
create table sometable
( id int auto_increment primary key,
userName varchar(40) not null,
ctPassword varchar(40) not null -- clear text password (means humans can read it, basically)
-- note, not a great definition of ct but it implies it has not been hashed for safety
);
insert sometable(userName,ctPassword) values
('Brenda','I watch TV too much'),
('Drew','PatriotsWorldChamps'),
('stealth_guy','JFIDU&JF_Anchovies');
随之而来的想法是,嘿,我现在想要安全的哈希值。我可能会被黑。
-- http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
alter table sometable add column hashedPassword varchar(255);
-- now I have 4 columns, hashedPassword is currently nullable
show create table sometable; -- confirms this fact
PHP 循环遍历并更新一个新列,意在在没有哈希概念之前进行清理(我认为我们都在堆栈上看到过 100 万次)
用于修补的 PHP:
<?php
// turn on error reporting, or wonder why nothing is happening at times
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//mysqli_report(MYSQLI_REPORT_ALL);
error_reporting(E_ALL);
ini_set("display_errors", 1); // Begin Vault
// credentials from a secure Vault, not hard-coded
$servername="localhost";
$dbname="login_system";
$username="dbUserName";
$password="dbPassword";
// End Vault
try {
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $db->prepare("select id,ctPassword from sometable");
$stmt->execute();
$stmt->bindColumn('id', $theId); // bind the results into vars by col names
$stmt->bindColumn('ctPassword', $cPassword); // ditto
// http://php.net/manual/en/pdostatement.fetch.php
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
// as we loop thru here, the $theId and $cPassword variables will be auto-magically updated
// for us because they have been bound as seen above
$hPassword=password_hash($cPassword,PASSWORD_DEFAULT); // we now have a hashed password based on orig clear text one
echo $cPassword . " " . $hPassword . "<br>";
// each time you run this with same data the hashes will be different due to changes in the salt
// based on above PASSWORD_DEFAULT (look at manual page for password_hash)
$sqlUpdate="UPDATE sometable set `hashedPassword`='$hPassword' where `id`=$theId";
$db->query($sqlUpdate);
}
// .. other cleanup as necessary
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit();
}
?>
运行php脚本,验证结果。那些是我的,你的将不同。如果您再次运行它,您的甚至会与您的不同。代码中提到的原因。
select * from sometable;
+----+-------------+---------------------+--------------------------------------------------------------+
| id | userName | ctPassword | hashedPassword |
+----+-------------+---------------------+--------------------------------------------------------------+
| 1 | Brenda | I watch TV too much | $2y$10$pJ5maui2OlrIPAtISf4u2OqeqEXU9ycDpCNNpp6xDh1uzIv/6ybuW |
| 2 | Drew | PatriotsWorldChamps | $2y$10$kHAKRSeHLi9cghPKTKox/.kXiFgq6ELWwExGcVvbf1yYprtTvi.Ba |
| 3 | stealth_guy | JFIDU&JF_Anchovies | $2y$10$HOkBAkP7ZVIZ7NQB50aKAuhG5WjLHU9AtJCiY2E6h/M2YZuxc2l5K |
+----+-------------+---------------------+--------------------------------------------------------------+
关于php - 使用哈希值更新数据库中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33962164/