해시 함수는 동일한 데이터를 입력 할 때 항상 동일한 결과를 반환한다는 것을 알고 있습니다. 하지만 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
으로 변경됩니까?
편집 : 내가 실수로 질문 이름이 암시로
이 사실입니다. 또한 .pre() 함수에서 코드 양을 반으로 줄입니다. 나는 소금이 어떻게 첨가되는지를 libsodium에서 추적하는 방법에 대해서는 언급하지 않을 것이라고 생각합니다. –