如何将我的盐存储为字符串,同时仍将其用作以后的缓冲区?
How can I store my salt as a string, while still using it as a buffer later?
我正在尝试对密码加盐,但收到以下错误消息:
(node:958) MaxListenersExceededWarning: Possible EventEmitter memory
leak detected. 11 exit listeners added. Use emitter.setMaxListeners()
to increase limit
TypeError: Salt must be a buffer
at pbkdf2 (crypto.js:644:20)
at Object.exports.pbkdf2 (crypto.js:624:10)
at model.exports.UserCredentialsSchema.methods.setPassword (/Users/friso/Documents/projects/MEANpress/server/src/schemas/user-credentials.schema.ts:35:5)
at App.setupMongoose (/Users/friso/Documents/projects/MEANpress/server/src/App.ts:42:15)
at new App (/Users/friso/Documents/projects/MEANpress/server/src/App.ts:14:14)
at Object.<anonymous> (/Users/friso/Documents/projects/MEANpress/server/src/server.ts:5:13)
at Module._compile (module.js:635:30)
at Module.m._compile (/Users/friso/Documents/projects/MEANpress/server/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/friso/Documents/projects/MEANpress/server/node_modules/ts-node/src/index.ts:442:12)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at Object.<anonymous> (/Users/friso/Documents/projects/MEANpress/server/node_modules/ts-node/src/bin.ts:157:12)
at Module._compile (module.js:635:30)
我正在尝试针对此架构和方法执行此操作:
export var UserCredentialsSchema: Schema = new Schema({
username: {
type: String,
lowercase: true,
unique: true
},
password: String,
salt: String
});
UserCredentialsSchema.methods.setPassword = function (password: string): void {
randomBytes(saltLength, (err, buf) => {
console.error(err);
this.salt = buf.toString();
});
pbkdf2(password, this.salt, hashIterations, hashLength, digest, (err, derivedKey) => {
console.error(err);
this.hashedPassword = derivedKey;
});
};
从在线文档和教程中我了解到 crypto 会自行将我的 salt 字符串转换为缓冲区,但这个错误让我不这么认为。
我是否遗漏了使用 pbkdf2
的任何步骤?
我在设置中尝试创建管理员用户时遇到错误:
const admin = new UserCredentials();
admin.username = 'admin';
admin.setPassword('admin');
admin.save();
Github 中的源代码链接:
如果您通过回调调用 randomBytes
(我假设它是 crypto.randomBytes
),则该过程是异步进行的。所以调用pbkdf2
时,this.salt
还没有初始化
将调用移到 randomBytes
' 回调中的 pbdkf2
,或者使用隐式同步版本:
try {
this.salt = randomBytes(saltLength);
} catch (err) {
// handle err here
}
我正在尝试对密码加盐,但收到以下错误消息:
(node:958) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added. Use emitter.setMaxListeners() to increase limit
TypeError: Salt must be a buffer
at pbkdf2 (crypto.js:644:20) at Object.exports.pbkdf2 (crypto.js:624:10) at model.exports.UserCredentialsSchema.methods.setPassword (/Users/friso/Documents/projects/MEANpress/server/src/schemas/user-credentials.schema.ts:35:5) at App.setupMongoose (/Users/friso/Documents/projects/MEANpress/server/src/App.ts:42:15) at new App (/Users/friso/Documents/projects/MEANpress/server/src/App.ts:14:14) at Object.<anonymous> (/Users/friso/Documents/projects/MEANpress/server/src/server.ts:5:13) at Module._compile (module.js:635:30) at Module.m._compile (/Users/friso/Documents/projects/MEANpress/server/node_modules/ts-node/src/index.ts:439:23) at Module._extensions..js (module.js:646:10) at Object.require.extensions.(anonymous function) [as .ts] (/Users/friso/Documents/projects/MEANpress/server/node_modules/ts-node/src/index.ts:442:12) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Function.Module.runMain (module.js:676:10) at Object.<anonymous> (/Users/friso/Documents/projects/MEANpress/server/node_modules/ts-node/src/bin.ts:157:12) at Module._compile (module.js:635:30)
我正在尝试针对此架构和方法执行此操作:
export var UserCredentialsSchema: Schema = new Schema({
username: {
type: String,
lowercase: true,
unique: true
},
password: String,
salt: String
});
UserCredentialsSchema.methods.setPassword = function (password: string): void {
randomBytes(saltLength, (err, buf) => {
console.error(err);
this.salt = buf.toString();
});
pbkdf2(password, this.salt, hashIterations, hashLength, digest, (err, derivedKey) => {
console.error(err);
this.hashedPassword = derivedKey;
});
};
从在线文档和教程中我了解到 crypto 会自行将我的 salt 字符串转换为缓冲区,但这个错误让我不这么认为。
我是否遗漏了使用 pbkdf2
的任何步骤?
我在设置中尝试创建管理员用户时遇到错误:
const admin = new UserCredentials();
admin.username = 'admin';
admin.setPassword('admin');
admin.save();
Github 中的源代码链接:
如果您通过回调调用 randomBytes
(我假设它是 crypto.randomBytes
),则该过程是异步进行的。所以调用pbkdf2
时,this.salt
还没有初始化
将调用移到 randomBytes
' 回调中的 pbdkf2
,或者使用隐式同步版本:
try {
this.salt = randomBytes(saltLength);
} catch (err) {
// handle err here
}