2016-03-29 5 views
1

Node.jsに関数があるとしましょう。両方のパラメータを返す正しい方法は何ですか?Node.jsの複数のパラメータを返す

たとえば、私は上記のコードのように暗号化されたメッセージを返す関数を持っています。また、これも返されたいHmacハッシュとそれを再現します。 1つの関数から両方の値を返すことはできますか?

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; 
}; 
+2

あなたは配列やオブジェクト以外の意味? – SmokeyPHP

+0

オブジェクトまたは配列に挿入するだけです。 –

答えて

2

ちょうどアレイを使用して2つの値を返すことができ:

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}; 
}; 
+2

技術的には、リスト/ディクショナリはJSを参照していません。配列とオブジェクトはここの用語です –

+0

@ StoneArcher十分に公正です。 :)私は用語を切り替えます。 –

関連する問題