返回Node.js

时间:2016-03-29 19:31:43

标签: javascript node.js encryption

我们假设我们在Node.js中有一个函数,返回两个参数的正确方法是什么?

例如,我有一个函数,它返回一个加密的消息,就像上面的代码一样,我也想返回它将被重现的Hmac哈希。我可以从一个函数返回两个值吗?

const crypto = require('crypto');
exports.AesEncryption = function(Plaintext, SecurePassword) {
    var cipher = crypto.createCipher('aes-128-ecb', SecurePassword);
    var encrypted = cipher.update(Plaintext, 'utf-8', 'base64');
    encrypted += cipher.final('base64');
    return encrypted;
};

1 个答案:

答案 0 :(得分:3)

只需使用数组即可返回两个值:

const crypto = require('crypto');
exports.AesEncryption = function(Plaintext, SecurePassword) {
    var cipher = crypto.createCipher('aes-128-ecb', SecurePassword);
    var encrypted = cipher.update(Plaintext, 'utf-8', 'base64');
    encrypted += cipher.final('base64');
    return [encrypted, cipher];
};

或对象(首选):

const crypto = require('crypto');
exports.AesEncryption = function(Plaintext, SecurePassword) {
    var cipher = crypto.createCipher('aes-128-ecb', SecurePassword);
    var encrypted = cipher.update(Plaintext, 'utf-8', 'base64');
    encrypted += cipher.final('base64');
    return {encrypted: encrypted, cipher: cipher};
};