简体   繁体   English

每次登录时更改数据库中的密码值

[英]Change password value in the database each time log in

Is it a good way to change password value each time a user log in to the database? 是用户每次登录数据库时更改密码值的好方法吗? I have wrote a hash function to hash the password when a user register a new account on the system. 当用户在系统上注册新帐户时,我编写了一个哈希函数来哈希密码。 Each time the user logs in, the hash value in the database will be changed. 每次用户登录时,数据库中的哈希值都会更改。 Is it good or bad? 是好还是坏?

If you designed this hash function all by your self then... It is a very very bad idea. 如果您完全由自己设计此哈希函数,那么...这是一个非常非常糟糕的主意。 Why would you need something like this? 你为什么需要这样的东西? If you store salted SHA-256 hashed passwords the security is good enough. 如果存储盐腌的SHA-256哈希密码,则安全性足够好。 You do not need to regenerate passwords, it does not provide any additional security. 您不需要重新生成密码,它不提供任何其他安全性。 If lets say your app is prone to SQL-Injection, then this scheme won't protect your app. 如果说您的应用易于进行SQL注入,那么该方案将无法保护您的应用。 You would be a lot better if you used salted and keyed SHA-256, something like this: (I'm not a php coder, I just want our apps to be secure) 如果您使用盐腌和键控的SHA-256,则可能会更好,例如:(我不是php编码器,我只是希望我们的应用程序是安全的)

$username = 'Admin';
$password = 'gf45_gdf#4hg';
$key = 'MySuperSecretKEY!!!!';
$salt = hash('sha256', uniqid(mt_rand(), true) . 'something random' . strtolower($username));
$hash = $salt . $password . $key;
$hash = hash('sha256', $hash);
$hash = $salt . $hash;

and then checking: 然后检查:

$username = 'Admin';
$password = 'gf45_gdf#4hg';

$sql = '
  SELECT
    `hash`
  FROM `users`
    WHERE
      `username` = "' . mysql_real_escape_string($username) . '"
  LIMIT 1
  ;';

$r = mysql_fetch_assoc(mysql_query($sql));

$salt = substr($r['hash'], 0, 64);
$hash = $salt . $password . $key;
$hash = hash('sha256', $hash);
$hash = $salt . $hash;

if ( $hash == $r['hash'] ) {
  //OK
}

So even if attacker will be able to trick the salting algorithm he does not know, a key so he won't be able to reproduce a valid hash in SQL-Injection attack. 因此,即使攻击者能够欺骗他不知道的盐析算法,还是一个密钥,这样他就无法在SQL-Injection攻击中复制有效的哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM