Mô tả
Phương thức cipher.final() dùng để trả về một buffer hoặc string chứa giá trị của đối tượng cipher. Đây là một trong những phương thức built-in do class Cipher trong module crypto cung cấp. Nếu output encoding được chỉ định, một String được trả về. Nếu output encoding không được chỉ định, một buffer được trả về. Gọi cipher.final nhiều hơn một lần sẽ ném lỗi.
Cú pháp
cipher.final([outputEncoding])
Tham số
outputEncoding – Nhận output encoding làm tham số. Kiểu đầu vào cho tham số này là string. Các giá trị có thể: hex, base64, v.v.
Ví dụ
Tạo file tên cipherFinal.js và sao chép đoạn mã dưới. Sau khi tạo file, dùng lệnh sau để chạy:
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);Đầu ra
C:\home\node>> node cipherFinal.js Hex String:- 8d11772fce59f08e7558db5bf17b3112 Base64 String:- jRF3L85Z8I51WNtb8XsxEg==
Ví dụ khác
Xem thêm một ví dụ nữa.
// 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); });Đầu ra
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)
Trong ví dụ trên, chúng ta gặp lỗi vì đã lấy cipher cho khóa đó rồi. Vì là phương thức final nên nó báo lỗi khi ta cố gắng tìm cipher cho cùng khóa lần nữa.