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)
在上述示例中,我们遇到了错误,因为我们已经获得了该密钥的密码。因为这是一个最终方法,所以当我们尝试再次查找同一密钥的密码时,它会出错。
广告