使用这个:https://github.com/ncb000gt/node.bcrypt.js/
bcrypt是关注这个用例的少数算法之一.您永远不能解密密码,只能验证用户输入的明文密码是否与存储/加密的哈希匹配.
bcrypt的使用非常简单.下面是我的Mongoose用户模式(CoffeeScript)的一个片段.请确保使用异步函数,因为bycrypt速度很慢(故意).
class User extends SharedUser
defaults: _.extend {domainId: null}, SharedUser::defaults
#Irrelevant bits trimmed...
password: (cleartext, confirm, callback) ->
errorInfo = new errors.InvalidData()
if cleartext != confirm
errorInfo.message = 'please type the same password twice'
errorInfo.errors.confirmPassword = 'must match the password'
return callback errorInfo
message = min4 cleartext
if message
errorInfo.message = message
errorInfo.errors.password = message
return callback errorInfo
self = this
bcrypt.gen_salt 10, (error, salt)->
if error
errorInfo = new errors.InternalError error.message
return callback errorInfo
bcrypt.encrypt cleartext, salt, (error, hash)->
if error
errorInfo = new errors.InternalError error.message
return callback errorInfo
self.attributes.bcryptedPassword = hash
return callback()
verifyPassword: (cleartext, callback) ->
bcrypt.compare cleartext, @attributes.bcryptedPassword, (error, result)->
if error
return callback(new errors.InternalError(error.message))
callback null, result
此外,阅读this article, which should convince you that bcrypt is a good choice,帮助你避免变得"精力充沛".