因此,我在MEAN Stack应用程序中使用随机盐字符串和密码字符串生成密码哈希。该程序成功生成了随机盐并将其打印在控制台上。但是,它卡在了pbkdf2Sync函数上,无法继续前进。
这是我的代码:
const crypto = require('crypto');
UserSchema.methods.setPassword = function(password){
console.log('start');
this.salt = crypto.randomBytes(16).toString('hex');
console.log(this.salt);
this.hash = crypto.pbkdf2Sync(new Buffer.alloc(password), new Buffer.alloc(this.salt), 1000, 64, 'sha512').toString('hex');
console.log(this.hash);
};
输出结果为:
start
ac09ae82b1fbb5b01257b5fa72bfc8614
然后程序就卡在这里了。
据我所知,该功能并未被弃用。我应该如何进行
This worked for me, essentially
alloc
is looking for the number of bytes (size to allocate in memory), a string wouldn't work here.If you're looking to use
password
andsalt
as filled Buffers (with values), then use the second argument (fill
), in the case of password:new Buffer.alloc(password.length, password)
.Instead, using the
.length
to return the amount of bytes (size of string), this generated the "correct" output:Ref: https://nodejs.org/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding