cipher.final() ใช้เพื่อส่งคืนบัฟเฟอร์หรือสตริงที่มีค่าของวัตถุเข้ารหัส มันเป็นหนึ่งในวิธีการ inbuilt ที่จัดเตรียมโดย class Cipher ภายในโมดูล crypto หากระบุการเข้ารหัสเอาต์พุต สตริงจะถูกส่งคืน หากไม่ได้ระบุการเข้ารหัสเอาต์พุต บัฟเฟอร์จะถูกส่งคืน การเรียกเมธอด cipher.final มากกว่าหนึ่งครั้งจะทำให้เกิดข้อผิดพลาด
ไวยากรณ์
cipher.final([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\node>> 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\node>> 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)
ในตัวอย่างข้างต้น เราได้รับข้อผิดพลาดเนื่องจากเราได้รับรหัสสำหรับคีย์นั้นแล้ว เนื่องจากเป็นวิธีสุดท้าย จึงเกิดข้อผิดพลาดเมื่อเราพยายามค้นหารหัสสำหรับคีย์เดียวกันอีกครั้ง