Node.jsのcipher.update()メソッド
cipher.update()は、指定されたエンコード形式に従って、受信したデータで暗号を更新するために使用されます。これは、暗号モジュール内のクラスCipherによって提供される組み込みメソッドの1つです。入力エンコーディングが指定されている場合、データ引数は文字列です。それ以外の場合、データ引数はバッファです
構文
cipher.update(data, [inputEncoding], [outputEncoding])
パラメータ
上記のパラメータは以下のように記述されます-
-
データ –暗号コンテンツを更新するために渡される入力としてデータを受け取ります。
-
inputEncoding –入力エンコーディングをパラメーターとして受け取ります。可能な入力値は、hex、base64などです。
-
outputEncoding –出力エンコーディングをパラメーターとして受け取ります。このパラメータの入力タイプは文字列です。可能な入力値は、hex、base64などです。
例
名前がcipherUpdate.jsのファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すようにこのコードを実行します-
node cipherUpdate.js
cipherUpdate.js
// Example to demonstrate the use of cipher.final() method
// Importing the crypto module
const crypto = require('crypto');
// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';
// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'old data', 24);
// Initializing the static iv
const iv = Buffer.alloc(16, 0);
// Initializing the cipher object to get cipher
const cipher = crypto.createCipheriv(algorithm, key, iv);
//Getting the updated string value with new data
let updatedValue = cipher.update('Welcome to tutorials point', 'utf8', 'hex');
//Adding the old value and updated value
updatedValue += cipher.final('hex');
// Printing the result...
console.log("Updated String:- " + updatedValue); 出力
C:\home\node>> node cipherUpdate.js Updated String:- a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a
例
もう1つの例を見てみましょう。
// Example to demonstrate the use of cipher.final() method
// Importing the crypto module
const crypto = require('crypto');
// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';
// Retrieving key for the cipher object
crypto.scrypt(password, 'salt', 24,
{ N: 512 }, (err, key) => {
if (err) throw err;
// Initializing the static iv
const iv = Buffer.alloc(16, 0);
// Initializing the cipher object to get cipher
const cipher = crypto.createCipheriv(algorithm, key, iv);
//Getting the updated string value with new data
let updatedValue = cipher.update('Some new text data', 'utf8', 'hex');
//Adding the old value and updated value
updatedValue += cipher.final('hex');
// Printing the result...
console.log("Updated String:- " + updatedValue);
}); 出力
C:\home\node>> node cipherUpdate.js Updated String:- 91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074
-
Node.jsのprocess.argv0()メソッド
process.argv0()メソッドは、node.jsアプリケーションの起動時に渡されるargv[0]の元の値の読み取り専用コピーを保存するために使用されます。 構文 process.argv0() パラメータ argv[0]のプリペイドカードの読み取り専用コピーのみを返すため。ユーザーからの入力は必要ありません。 例 argv0.jsという名前のファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すようにこのコードを実行します- node argv0.js argv0.js // Node.js program to
-
Node.jsのprocess.argv()メソッド
process.argv()メソッドは、Node.jsプロセスの起動時に渡されたすべてのコマンドライン引数を返すために使用されます。最初の要素には、常にprocess.execPathと同じ値が含まれます。 構文 process.argv() パラメータ node.jsプロセスの前に渡されたすべてのコマンドライン引数を返すため。ユーザーからの入力は必要ありません。 例 argv.jsという名前のファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すようにこのコードを実行します- node argv.js argv.js