使用哈希值更新DataBase中的所有行

时间:2015-11-27 17:18:51

标签: php mysql database

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

EG。 :在列“名称”中有一行包含“MyName”,

这是我试图实现的方案&逻辑:

  1. 循环数据库

  2. 获取每行的当前值&用哈希('...',值)哈希吧;

  3. 获取每行的现有值&散列它&更新,这是我想要更新完整数据库的方式,

    我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

首先我要说的是,如果你在db中有非敏感数据,那么内置的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的明文列。您可以alter tablehashedPassword之类的内容添加新列。按照我提供的链接进行相应调整,使用更新语句将ctPassword的值哈希到hashedPassword

然后彻底测试。当一切都在世界上时,放弃 ctPassword列,永远不再使用它。 要清楚,切勿在数据库中存储明文密码。存储单向哈希值,并验证它们。以上链接显示了如何。

修改

这里完全来自PHP,我认为这需要驱动,而不是mysql哈希函数,yuck。毕竟,你正在使用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循环并更新一个新列,意味着在没有哈希概念之前进行清理(我认为我们已经在堆栈上看到了1M次)

用于补丁的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 |
+----+-------------+---------------------+--------------------------------------------------------------+
相关问题