Node.js 中的 cipher.final() 方法


cipher.final() 用來返回一個 buffer 或者包含 cipher 物件值的字串。它是加密模組的 Cipher 類中提供的內建方法之一。如果指定了輸出編碼,則會返回一個 String。如果未指定輸出編碼,則會返回一個 buffer。多次呼叫 cipher.final 方法會引發錯誤。

語法

cipher.final([outputEncoding])

引數

以上引數說明如下 −

  • outputEncoding – 它將輸出編碼作為引數。此引數的輸入型別為字串。可能的輸入值有 hex、base64 等。

示例

建立一個名為 cipherFinal.js 的檔案,並複製以下程式碼片段。建立檔案後,使用以下命令執行此程式碼,如下面的示例所示 −

node cipherFinal.js

cipherFinal.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 = '12345678';

// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'salt', 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);
const cipher2 = crypto.createCipheriv(algorithm, key, iv);

//Getting the string value as outputEncoding is defined
let hexValue = cipher.final('hex');
let base64Value = cipher2.final('base64');

// Printing the result...
console.log("Hex String:- " + hexValue);
console.log("Base64 String:- " + base64Value)

輸出

C:\home
ode>> node cipherFinal.js Hex String:- 8d11772fce59f08e7558db5bf17b3112 Base64 String:- jRF3L85Z8I51WNtb8XsxEg==

示例

我們再來看看另一個示例。

// 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 = '12345678';

// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'salt', 24);

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 buffer value since output encoding is null
   let hexValue = cipher.final();
   let base64Value = cipher.final('base64');

   // Printing the result...
   console.log("Buffer:- " + hexValue);
   console.log("Base64 String:- " + base64Value)
});

輸出

C:\home
ode>> node cipherFinal.js internal/crypto/cipher.js:164    const ret = this._handle.final();                            ^ Error: Unsupported state    at Cipheriv.final (internal/crypto/cipher.js:164:28)    at Object. (/home/node/test/cipher.js:22:26)    at Module._compile (internal/modules/cjs/loader.js:778:30)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)    at Module.load (internal/modules/cjs/loader.js:653:32)    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)    at Function.Module._load (internal/modules/cjs/loader.js:585:3)    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)    at startup (internal/bootstrap/node.js:283:19)    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

在上述示例中,我們遇到了錯誤,因為我們已經獲得了該金鑰的密碼。因為這是一個最終方法,所以當我們嘗試再次查詢同一金鑰的密碼時,它會出錯。

更新於: 2021 年 5 月 20 日

605 次瀏覽

開啟你的 職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.