假设我们在 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;
};
最佳答案
您可以只使用数组返回两个值:
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};
};
关于javascript - 在 Node.js 中返回多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36293491/