From ec206db1732f2960b9f85fa2e6ab915744e8f5a0 Mon Sep 17 00:00:00 2001 From: BinotaLIU Date: Thu, 9 Apr 2020 02:26:25 +0800 Subject: [PATCH] add methods for password hashing in User model Signed-off-by: BinotaLIU --- lib/models/user.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/models/user.js b/lib/models/user.js index 3d84f91c..d1e43ff8 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -1,7 +1,7 @@ 'use strict' // external modules var Sequelize = require('sequelize') -var scrypt = require('scrypt') +var Scrypt = require('scrypt-kdf') // core var logger = require('../logger') @@ -41,20 +41,20 @@ module.exports = function (sequelize, DataTypes) { } }, password: { - type: Sequelize.TEXT, - set: function (value) { - var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex') - this.setDataValue('password', hash) - } + type: Sequelize.TEXT } }) - User.prototype.verifyPassword = function (attempt) { - if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) { + User.prototype.hashPassword = async function (plain) { + return Scrypt.kdf(plain, await Scrypt.pickParams(0.1)).toString('hex') + } + + User.prototype.verifyPassword = async function (attempt) { + if (await Scrypt.verifyKdf(Buffer.from(this.password, 'hex'), attempt)) { return this - } else { - return false } + + return false } User.associate = function (models) {