使用哈希值更新数据库中的所有行


Update All Rows in DataBase with a hash value

使用Php&MySQL,我想更新数据库中特定列的所有行,

例如:在"名称"列中有一行包含"MyName",

这是我的计划,我正在努力实现&逻辑:

  1. 循环数据库

  2. 获取每行的当前值&用hash("…",value)对其进行散列;

获取每行的现有值&散列&更新,这就是我想如何更新完整的数据库,

我怎样才能做到这一点?

首先,我必须说,如果数据库中有非敏感数据,那么内置的mysql函数可以直接使用mysql通过更新语句为您提供哈希结果。

这个答案与此无关。它是关于敏感数据的,比如密码。

我给了你一个链接到PHP password_hash()password_verify()的例子。

这又是那个链接。左边的链接是PDO的。以下链接与mysqli类似。

在PDO链接中,查看行

$hp=password_hash($ctPassword,PASSWORD_DEFAULT); // hashed password, using 

假设您现在有一个列,其中包含明文,称为ctPassword。您需要alter table,并为类似hashedPassword的内容添加一个新列。按照我提供的链接,进行相应的调整,用update语句将ctPassword的值散列到hashedPassword中。

然后彻底测试。当一切正常时,删除ctPassword列,然后再也不使用它为了清楚,永远不要在数据库中存储明文密码。存储单向散列值,并对其进行验证。以上链接显示了如何操作。

编辑

这里完全来自PHP,我认为这需要从PHP驱动,而不是mysql散列函数,真是太棒了。毕竟,您正在使用PHP,正是在那里,他们强大的哈希和验证将大放异彩。在我看来,最佳实践,而mysql的人并没有花太多的精力在这上面。我完全支持在mysql中尽可能多地做。但千万不要用这个话题,用hashes。让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 |
+----+-------------+---------------------+--------------------------------------------------------------+