3

当用户注册时,我获取他们的密码并使用哈希和盐对其进行保护,然后将其存储到 mysql DB 中。那么当注册用户尝试登录时,我如何检查密码是否有效?

我能想到的唯一方法是获取他们的密码,哈希+盐,然后检查哈希+盐是否在数据库中。我不确定这是如何安全的?因为这意味着我的代码将对任何密码进行哈希+加盐并在数据库中找到匹配项。

我读到了每次点击登录页面时都会生成随机盐,但这并不意味着如果注册用户尝试登录,则会生成一个新的哈希值,该哈希值与同一用户注册时生成的哈希值不同。

如果有人能阐明这一点,我将不胜感激。我正在使用 Java。

4

2 回答 2

2

basically you have to store h(password+salt) and salt in the database. If some user tries to log in, you'll get his plain-text password. Then you get the salt of the user from you database, generate the hash of the password+salt, and if its the same as the stored hash, the authentication was successful.

This is more secure than a basic hashed password, because it makes mass-bruteforcing from the hash a bit harder, because the attacker has to guess both the salt and the actual password. However, this does not make bruteforcing a single password harder, if the salts are public (or known to the attacker). It also makes rainbow-tables useless, which are massive lists of pregenerated hashes for common passwords

To get a deeper explanation, look here or here

于 2013-06-09T18:30:38.983 回答
0

在我的情况下,我将散列(盐+密码)和盐存储在数据库中,当用户提供登录名和密码时,我得到盐,添加到提供的密码,然后散列并与存储的散列密码进行比较来自数据库. 如果相等/正确,我会重新生成一个新盐并存储,然后将新盐添加到正确的密码中,然后散列并存储。

于 2018-08-09T03:14:28.090 回答