cipher.final()은 cipher 객체의 값을 포함하는 버퍼 또는 문자열을 반환하는 데 사용됩니다. 암호화 모듈 내에서 Cipher 클래스가 제공하는 내장 메소드 중 하나입니다. 출력 인코딩이 지정되면 문자열이 반환됩니다. 출력 인코딩이 지정되지 않으면 버퍼가 반환됩니다. cipher.final 메소드를 두 번 이상 호출하면 오류가 발생합니다.
구문
cipher.final([outputEncoding])
매개변수
위의 매개변수는 다음과 같이 설명됩니다. -
-
출력인코딩 – 출력 인코딩을 매개변수로 사용합니다. 이 매개변수의 입력 유형은 문자열입니다. 가능한 입력 값은 16진수, 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)
위의 예에서는 해당 키에 대한 암호를 이미 가지고 있기 때문에 오류가 발생합니다. 마지막 방법이기 때문에 동일한 키에 대한 암호를 다시 찾으려고 하면 오류가 발생합니다.