我正在寻找一些如何使用node.js和mongodb安全地存储密码和其他敏感数据的示例。
我希望所有东西都使用唯一的盐,我会将其与mongo文档中的哈希值一起存储。
对于身份验证,我是否只需要对输入进行盐分和加密,并将其与存储的哈希值进行匹配?
我需要解密这些数据吗?如果需要,我应该怎么做?
私钥,甚至加盐方法是如何安全地存储在服务器上的?
我听说AES和Blowfish都是不错的选择,我应该用什么?
任何关于如何设计它的例子都会非常有帮助!
谢谢!
发布于 2011-08-05 12:54:48
使用这个: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,并帮助你避免成为“好的和真正的上帝”。
发布于 2012-09-23 04:06:07
这是我迄今为止遇到的最好的例子,使用node.bcrypt.js http://devsmash.com/blog/password-authentication-with-mongoose-and-bcrypt
https://stackoverflow.com/questions/6951563
复制相似问题