为什么compareSync不需要盐串?

Pra*_*vin 7 hash salt bcrypt saltedhash node.js

我正在尝试使用bcryptjs生成用户密码的哈希值.但是我在一件事情上有点困惑.

按照惯例,根据这一条款,我们需要:

  • 保持我们的密码哈希值相对较长且独特,
  • 使用此盐对用户密码进行哈希处理
  • 存储盐渍哈希密码和盐

因此,当我们在验证用户时比较哈希时,我们将存储的盐附加到用户输入的密码,并将其与来自数据库的哈希进行比较.

然而,使用hashSync和compareSync bcryptjs如下:

//hashSync to generate hash
var bcrypt = require('bcryptjs');
var password = "abc";
var hash = bcrypt.hashSync( <some string>, < integer length of salt>) // the salt of mentioned length(4-31) is self-generated which is random and fairly unique

//compareSYnc to compare hash
var testString="abc";
console.log(bcrypt.compareSync(testString, hash)) // compares with previously generated hash returns "true" in this case.
Run Code Online (Sandbox Code Playgroud)

我感到困惑的是,如果我们在验证时不需要盐,那么产生它的意义何在?compareSync返回时true无需访问salt.所以它不会让相对较小的密码容易遭受暴力攻击吗?无论盐的大小如何,以下所有内容都返回true:

console.log(bcrypt.compareSync("abc", bcrypt.hashSync("abc"))); // consoles true. by default, if salt size is not mentioned, size is 10.
console.log(bcrypt.compareSync("abc", bcrypt.hashSync("abc", 4))); //consoles true
console.log(bcrypt.compareSync("abc", bcrypt.hashSync("abc", 8))); //consoles true
console.log(bcrypt.compareSync("abc", bcrypt.hashSync("abc", 32))); //consoles true
console.log(bcrypt.compareSync("ab", bcrypt.hashSync("abc", 4))); //consoles false
Run Code Online (Sandbox Code Playgroud)

我希望我能够清楚地解释我的困惑.

Aar*_*our 8

bcrypt标准使存储盐变得容易 - 检查密码所需的一切都存储在输出字符串中.

影子密码文件中的散列字符串中的前缀"$ 2a $"或"2y"表示散列字符串是模块化密码格式的bcrypt散列.哈希字符串的其余部分包括cost参数,128位salt(base-64编码为22个字符)和192位[dubious - discuss]哈希值(base-64编码为31个字符).

这是来自bcrypt上的维基百科页面.

  • @Pravin哈希1个字符的密码和33个字符的密码之间的区别很小.salt _is_存储 - 它只存储在包含哈希的相同字符串中.盐只能阻止彩虹表攻击.这就是他们的全部. (5认同)