Computer >> Máy Tính >  >> Lập trình >> Javascript

Phương thức cipher.final () trong Node.js

Cipher.final () được sử dụng để trả về bộ đệm hoặc chuỗi chứa giá trị của đối tượng mật mã. Nó là một trong những phương thức có sẵn được cung cấp bởi Class Cipher trong mô-đun tiền điện tử. Nếu một mã hóa đầu ra được chỉ định, một Chuỗi sẽ được trả về. Nếu một mã hóa đầu ra không được chỉ định, một bộ đệm sẽ được trả về. Gọi phương thức cipher.final nhiều lần sẽ gây ra lỗi.

Cú pháp

cipher.final([outputEncoding])

Tham số

Các thông số trên được mô tả như bên dưới -

  • outputEncoding - Nó lấy mã hóa đầu ra làm tham số. Loại đầu vào cho tham số này là chuỗi. Các giá trị đầu vào có thể có là hex, base64, v.v.

Ví dụ

Tạo một tệp có tên - cipherFinal.js và sao chép đoạn mã bên dưới. Sau khi tạo tệp, sử dụng lệnh sau để chạy mã này như được hiển thị trong ví dụ bên dưới -

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ụ

Hãy xem thêm một ví dụ.

// 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 tôi gặp lỗi vì chúng tôi đã có mật mã cho khóa đó. Vì đây là phương pháp cuối cùng nên nó sẽ xuất hiện lỗi khi chúng tôi cố gắng tìm lại mật mã cho cùng một khóa.