javascript 用于散列密码的Bcrypt助手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 用于散列密码的Bcrypt助手相关的知识,希望对你有一定的参考价值。

'use strict'

const bcrypt = require('bcrypt')

const BCRYPT_SALT_ROUNDS = 10

/**
 * Creates a hash of a string using default bcrypt config
 * 
 * @param  {String} password Password or any string to be hashed
 * @return {Promise}         Error or {String} hash
 */
const createHash = password =>
    // return bcrypt.hashSync(password, bcrypt.genSaltSync(BCRYPT_SALT_ROUNDS), null)
    bcrypt.hash(password, BCRYPT_SALT_ROUNDS)

/**
 * Validates a string against a hash
 * 
 * @param  {String} password Password or any string to compare with [hash]
 * @param  {String} hash     Hash to compare with [password]
 * @return {Boolean}         True if hash & hashed password match
 */
const validateHash = (password, hash) =>
    // return bcrypt.compareSync(password, this.password)
    bcrypt.compare(password, hash)

module.exports.createHash = createHash
module.exports.validateHash = validateHash

以上是关于javascript 用于散列密码的Bcrypt助手的主要内容,如果未能解决你的问题,请参考以下文章