gpt4 book ai didi

node.js - 我对密码哈希有什么误解?

转载 作者:太空宇宙 更新时间:2023-11-04 00:27:04 25 4
gpt4 key购买 nike

据我了解,当输入相同的数据时,哈希函数将始终返回相同的结果。但我一直在使用 libsodium (通过 node-sodium),但事实并非如此。

我的架构中有这个:

UserSchema.pre('save', function(next) {
// declare my variables
let user = this,
buf = Buffer.alloc(sodium.crypto_pwhash_STRBYTES, 'ascii'),
passwordBuf = Buffer.from(user.password, 'ascii'),
saltedPassBuf,
hash;
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) return next();
// generate a salt
sodium.randombytes_buf(buf, sodium.crypto_pwhash_STRBYTES, 'ascii');
// add salt to the password
saltedPassBuf = Buffer.concat([passwordBuf, buf], 128);
// hash it separately multiple times
// note, i'm not hashing the hash,
// I'm hashing the original buffer to see what happens
// this has no application in production
hash = sodium.crypto_pwhash_str(saltedPassBuf, sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE);
hash2 = sodium.crypto_pwhash_str(saltedPassBuf, sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE);
hash3 = sodium.crypto_pwhash_str(saltedPassBuf, sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE);
// log it to see what I got -- not for production
console.log(hash.toString());
console.log(hash2.toString());
console.log(hash3.toString());
// save the salt and the buffer for authentication
user.salt = buf;
user.password = hash;
next();
});

我用该代码记录了三个不同的字符串。例如

$argon2i$v=19$m=32768,t=4,p=1$ayPVQ1X+xNhWmD9S5AUuaw$1mWusk59AebhzOHhl+j5JpvmRI27Pq57XG5zcAB5R4U
$argon2i$v=19$m=32768,t=4,p=1$PjTYKpfhh1bZh+MV84Y9kA$9+U33nf6efuugsrz15cEKDa5+rAHgYVA5Kqo4F1G3DE
$argon2i$v=19$m=32768,t=4,p=1$Ii8AErmAFc0na9Yi2OgCkw$ySU80Fv9OiOmeT9EV/BWon1Jjck2Lx23nOeCk0wkMPU

现在,每个部分的第一部分都是相同的,这让我觉得提交的密码部分是相同的(因为它是正在散列的缓冲区的第一部分)。所以也许这是我不明白的缓冲区。

但是如果 buf 保持静态,为什么 saltedPassBuff 的其余部分会发生变化?

编辑:不小心提交的时候还没写完,编辑完把问题写完了

最佳答案

除了您的盐之外,pwhash 函数(文档很少)很可能还会添加自己的随机盐,该盐也包含在结果中,以便以后使用 crypto_pwhash_str_verify 进行比较.

还有一个“CPU 密集型”方面,可能是迭代。仅使用带有盐的哈希函数对于提高安全性几乎没有作用。需要添加 CPU 密集型组件,例如迭代。

重点是让攻击者花费大量时间通过暴力方式查找密码。

关于node.js - 我对密码哈希有什么误解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42367551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com