在bcrypt中加盐的要点 [英] The point with the salt in bcrypt

查看:422
本文介绍了在bcrypt中加盐的要点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个愚蠢的问题,我只想知道:bcrypt中的盐有什么意义?我的意思是,如果您具有以下用于通过密码创建哈希的代码:

Sorry if this is a stupid question, I just want to know: what is the point with the salt in bcrypt? I mean, if you have the following code for creating a hash from a password:

function generateSalt() {
$salt = '$2a$13$';
$salt = $salt . '1111111111111111111111';
return $salt;
}

function generateHash($salt, $password) {
$hash = crypt($password, $salt);

return $hash;
}

$salt = generateSalt();

$providedPassword = generateHash($salt, rand(3,29));

echo $providedPassword;

上面的输出例如:

$ 2a $ 13 $ 111111111111111111111uDdpsIcwCVOwEyNueskskXkniY5206fW

$2a$13$111111111111111111111uDdpsIcwCVOwEyNueskskXkniY5206fW

$ 2a $ 13 $ 111111111111111111111udcvrNt9quPukFRl8./jXRzDGfE9lw0W

$2a$13$111111111111111111111udcvrNt9quPukFRl8./jXRzDGfE9lw0W

因此,您可以清楚地看到salt的结尾,并且如果有人获得了数据库,就不会对salt毫无意义,因为他们只能删除salt部分并仅搜索哈希密码.那么,我使用bcrypt是错误的吗? (静态盐只是为了显示它在我的哈希表中的位置),或者这是有原因的吗?

So you can clearly see where the salt ends, and if someone gets the database there's not point with the salt, since they just can remove the salt-part and search for just the hashed password. So, am I using bcrypt wrong? (the static salt was just to show where it appears in my hashes), or is there a reason with this?

推荐答案

salt 是即使两个输入相同,只要每次使用不同的盐,哈希值就不会相同.

The idea behind a salt is that even if two inputs are the same, the hash will not be identical as long as a different salt is used every time.

例如,许多用户选择相同的密码.如果仅存储密码的哈希,则数据库将包含许多相同的哈希值-因此,如果攻击者仅一次找到密码,则攻击者可以轻松地将密码用于所有这些用户.但是,如果为每个用户使用不同的盐值对密码进行散列,则攻击者将不得不破解存储在存储中的每个散列.

For example, many users pick the same password. If you just store the hash of the password, the database will contain many identical hashes - so that if an attacker finds the password just once, he can then use it for all those users easily. However, if the password is hashed with a different salt value for each user, the attacker will have to crack each and every hash stored in the store.

我不确定您正在使用的代码是什么(crypt函数是什么?),但是只要将盐值添加到实际的哈希值中就可以了,只要哈希值本身也可以计算出来用盐.无论如何,您都将需要存储原始盐,以验证新输入(密码)是否与存储的哈希匹配.但是,只要您在每次使用哈希之间都更改salt值,就没有简便的方法来收集有关原始输入的信息.

I'm not sure what's that code you're using (what's that crypt function?), but it's ok if it prepends the salt value to the actual hash as long as the hash itself is also calculated using the salt. You're going to need to store the original salt anyway to verify that a new input (password) matches the stored hash. However, as long as you change the salt values between every hash usage, there's no easy way to glean information about the original input.

这篇关于在bcrypt中加盐的要点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆