crypto pbkdf2未被调用

时间:2017-07-07 09:10:07

标签: node.js pbkdf2

我已经用crypto.obkdf2编写了一个hashpasswords函数。这个功能工作正常,但由于某种原因他再也没有了。

修改

在控制器中,我想用user.service创建一个用户:

account.controller:

userService.createAccount(details, true, function(err, result){
    if(err){
        res.status(500).json({message: err});
    }else{
        //account is created, details are mailed, now we can set the user on the order  
        user = result;
    }
})

该服务执行一些检查并调用user.register函数

user.service

function createAccount (details, generatePassword, callback){
    console.log("createAccount");
    //generate password, save details + password, mail details, send results back in callback
    //if(generatePassword){
    //    details.password = random(10);
    //}
    details.password = '7eb4f68ea4';

    var userData = _.pick(details, 'firstName', 'lastNamePrefix', 'lastName','email','gender', 'password');
    userData.verificationCode = random(8);
    userData.userName = userData.email;
    console.log("userdate", userData);
    User.register(userData, function(err, user){
        console.log("register user", err);

        if(err && (11000 == err.code || 11001 == err.code)) {
            console.log("E-mailadres already in use");
            return callback("E-mailadres already in use");
        }

        if (err) {
            console.log("Something went terribly wrong...", err);
            return callback("Something went wrong: " + err)
        }
        var options = {
            to: user.email,
            subject: 'Uw account op Prisma Note',
            template: 'new-account'
        }
        //Mail sturen met activatielink na aanmaken
        mailService.mail(options, user, null, function(err, data){
            console.log("sendmail!");
            if (err) {
                console.log("error sending mail", err);
                return callback("Error sending mail: " + err.message);
            }else{
                console.log("hier!", user);
                return callback(null, user);
            }
        });

    })
}

user.modal包含注册函数:

user.model

User.statics.register = function(opts, callback) {
    console.log("register!");
    var self = this;
    var data = _.cloneDeep(opts);


    passwordService.hash(opts.password, function(err, hashedPassword, salt) {
        if (err) {
            console.log("passwordsrv", err);
            return callback(err, null);
        }

        data.password = hashedPassword;
        data.passwordSalt = salt;
        self.model('User').create(data, function(err, user) {
            if (err) {
                console.log("createuser", err);
                return callback(err, null);
            }

            user.password = undefined;
            user.passwordSalt = undefined;

            callback(err, user);
        })
    })
}

最后,代码来到了passwordService

password.service

var LEN = 256;
var SALT_LEN = 64;
var ITERATIONS = 10000;
var DIGEST = 'sha256';

function hashPassword(password, salt, callback){
    console.log("hashPassword");
    var len = LEN /2;
    if (3 === arguments.length) {
        crypto.pbkdf2(password, salt, ITERATIONS, len, DIGEST, function(err, derivedKey) {
            if (err) {
                console.log("crypto err", err);
                return callback(err);
            }

            return callback(null, derivedKey.toString('hex'));
        })
    } else {
        console.log("pwdsrv");
        callback = salt;
            crypto.randomBytes(SALT_LEN / 2, function(err, salt) {
                if (err) {
                    console.log("randombyte", err);
                    return callback(err);    
                }

                salt = salt.toString('hex');
                console.log("hier");

                console.log(password, salt, ITERATIONS, len, DIGEST);
                //password = '7eb4f68ea4'
                //salt = '21aaac28b8411326b8df5b8b60476904f58d40f972a2f5d698ec4d0fb57dc315'
                //ITERATIONS = 10000
                //len = 128
                //DIGEST = 'sha256'
                crypto.pbkdf2(password, salt, ITERATIONS, len, DIGEST, function(err, derivedKey) {
                    console.log("hjioo");
                    if (err) { 
                        console.log("pbkdf2", err);
                        return callback(err);
                    }
                    console.log("hier");
                    callback(null, derivedKey.toString('hex'), salt);
                })
            })
    }
}

此功能是从我们的用户模型'中调用的。该模型具有我们可以调用的User.statics.register函数。在经过大量调试后,我发现代码在函数的crypto.pbkdf2(password, salt, ITERATIONS, len, DIGEST, function(err, derivedKey)中停止(在我测试的情况下)else。此函数之前的日志显示变量的内容,但我无法在函数中看到日志。

我不知道,为什么停止工作。我们更新了node.JS版本,但由于这是一个节点模块,所以一定没问题。

有人可以帮我解决这个问题,所以我们的用户可以再次注册吗?

您可以在服务中看到的邮件服务通过Amazon SES发送电子邮件,我认为与发布该代码无关。

0 个答案:

没有答案