最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

我如何储存我的盐作为一个字符串,同时仍然使用它作为以后的缓冲?

IT培训 admin 5浏览 0评论

我如何储存我的盐作为一个字符串,同时仍然使用它作为以后的缓冲?

我试图盐密码,但我得到以下错误信息:

(节点:958)MaxListenersExceededWarning:检测可能EventEmitter内存泄漏。 11个退出听众加入。使用emitter.setMaxListeners()以增加限制

类型错误:盐必须是一个缓冲

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;
    });
};

从文档和教程在网上我明白了加密本身将我的盐的字符串转换为缓冲区,但这个错误让我认为并非如此。

我缺少使用pbkdf2也许任何步骤?

我得到的错误,而试图创建在设置管理员用户:

const admin = new UserCredentials();
admin.username = 'admin';
admin.setPassword('admin');
admin.save();

链接到Github的源代码:

  • Schema
  • Set the admin
回答如下:

如果调用randomBytes(我假设它是crypto.randomBytes)有一个回调,则处理异步的。所以,当pbkdf2被调用时,this.salt尚未初始化。

无论是移动调用pbdkf2”回调里面randomBytes,或者使用隐式同步版本:

try {
  this.salt = randomBytes(saltLength);
} catch (err) {
  // handle err here
}

我如何储存我的盐作为一个字符串,同时仍然使用它作为以后的缓冲?

我试图盐密码,但我得到以下错误信息:

(节点:958)MaxListenersExceededWarning:检测可能EventEmitter内存泄漏。 11个退出听众加入。使用emitter.setMaxListeners()以增加限制

类型错误:盐必须是一个缓冲

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;
    });
};

从文档和教程在网上我明白了加密本身将我的盐的字符串转换为缓冲区,但这个错误让我认为并非如此。

我缺少使用pbkdf2也许任何步骤?

我得到的错误,而试图创建在设置管理员用户:

const admin = new UserCredentials();
admin.username = 'admin';
admin.setPassword('admin');
admin.save();

链接到Github的源代码:

  • Schema
  • Set the admin
回答如下:

如果调用randomBytes(我假设它是crypto.randomBytes)有一个回调,则处理异步的。所以,当pbkdf2被调用时,this.salt尚未初始化。

无论是移动调用pbdkf2”回调里面randomBytes,或者使用隐式同步版本:

try {
  this.salt = randomBytes(saltLength);
} catch (err) {
  // handle err here
}
发布评论

评论列表 (0)

  1. 暂无评论