【问题标题】:How do I decrypt password using crypto hash and salt with my function in mongoose?如何在猫鼬中使用加密哈希和盐解密密码?
【发布时间】:2017-03-10 23:19:27
【问题描述】:

我有一组服务器端功能, 1.

  1. 生成随机盐
  2. 使用密码和盐来创建哈希
  3. 调用第二个函数创建返回的散列密码

    var sha512 = function(password, salt){
        var hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */
        hash.update(password);
        var value = hash.digest('hex');
        return {
            salt:salt,
            passwordHash:value
        };
    };
    
    
    //--------------Function to hash password
    function saltHashPassword(userpassword) {
        var salt = genRandomString(16); /** Gives us salt of length 16 */
        var passwordData = sha512(userpassword, salt);
        console.log('UserPassword = '+userpassword);
        console.log('Passwordhash = '+passwordData.passwordHash);
        console.log('\nSalt = '+passwordData.salt);
        return passwordData;
    }
    //----------------------
    // Function to generate salt
    var genRandomString = function(length){
        return crypto.randomBytes(Math.ceil(length/2))
            .toString('hex') /** convert to hexadecimal format */
            .slice(0,length);   /** return required number of characters */
    };
    

每当我在注册页面中获得用户名和密码时,我都可以调用 saltHashPassword(passwordinstring) 并获得哈希和盐。然后我将它保存到猫鼬模式中的用户文档中。

当用户尝试登录时如何解密?

这是我目前在用户模式 ​​user.js 中编写的方法。我不知道如何为提供的电子邮件存储盐。

// this authenticates the user against the database
UserSchema.statics.authenticate = function(email, password, callback){
    User.findOne({email: email})
        .exec(function(error, user){
            if(error){
                return callback(error);
            }else if(!user){
                var err = new Error("User not found");
                err.status = 401;
                return callback(err);
            }
            // if we reach this part of the code, then there is a valid username
            // I could call sha512(password, salt) and get the hash and then
            // match the two calculated hashed passwords. But how do 
            // I query the salt within a mongo query?
            // every user has email, hashed_password, salt fields in its document

        });
};

【问题讨论】:

    标签: node.js mongodb encryption hash


    【解决方案1】:

    看起来您正在尝试重新发明轮子。您可以使用 bcrypt,它不需要您单独存储盐。

    但既然你已经开始这样做了:

    // this authenticates the user against the database
    UserSchema.statics.authenticate = function(email, password, callback){
        User.findOne({email: email})
            .exec(function(error, user){
                if(error){
                    return callback(error);
                }
                if(!user){
                    var err = new Error("User not found");
                    err.status = 401;
                    return callback(err);
                }
                // if we reach this part of the code, then there is a valid username
                // I could call sha512(password, salt) and get the hash and then
                // match the two calculated hashed passwords. But how do 
                // I query the salt within a mongo query?
                // every user has email, hashed_password, salt fields in its document
    
    
                var salt = user.salt; // Because you said that User has salt, email, hashed_password
    
                // Do your password matching....
            });
    };
    

    Mongoose 的 find 和 findOne 方法返回错误或文档。您的回调中已经有错误和用户。您可以通过 user.email、user.salt 等访问属性。

    您不需要任何单独的查询,因为用户包含盐。

    【讨论】:

    • 你是说我应该这样做let salt = user.salt; let givenPassObj = sha512(password, salt); if(givenPassObj.passwordHash===user.hashed_password){ return callback(null, user); }else{ return callback(); }
    • 是的。应该这样做。
    • @McFiddlyWiddly 不,不要那样做。使用 SHA512 散列对于密码来说是不安全的。使用帖子中所述的 bcrypt,或者如果您真的想使用 SHA512,请将其用作 PBKDF2 的原语。
    • 是的,这就是为什么我说“就像帖子中所说的那样”;)我向 OP 致辞。
    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2017-03-08
    • 2010-11-30
    • 1970-01-01
    相关资源
    最近更新 更多