Node.js 中的 cipher.update() 方法
cipher.update() 用于根据给定的编码格式更新已接收加密数据。它是由 crypto 模块中的 Cipher 类提供的一个内置方法。如果指定了一个输入编码,那么数据参数是一个字符串,否则数据参数是一个缓冲区
语法
cipher.update(data, [inputEncoding], [outputEncoding])
参数
以上参数描述如下 −
data – 它将数据作为一个输入,该数据传递给更新加密内容。
inputEncoding – 它将输入编码作为一个参数。可能的输入值是十六进制、base64 等。
outputEncoding – 它将输出编码作为一个参数。该参数的输入类型是一个字符串。可能的输入值是十六进制、base64 等。
示例
创建一个名为 cipherUpdate.js 的文件,并复制下面的代码段。创建文件后,使用以下命令运行此代码,如下例所示 −
node cipherUpdate.js
cipherUpdate.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 = '12345678123456789';
// Retrieving key for the cipher object
const key = crypto.scryptSync(password, 'old data', 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);
//Getting the updated string value with new data
let updatedValue = cipher.update('Welcome to tutorials point', 'utf8', 'hex');
//Adding the old value and updated value
updatedValue += cipher.final('hex');
// Printing the result...
console.log("Updated String:- " + updatedValue);输出
C:\home
ode>> node cipherUpdate.js Updated String:- a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a
示例
我们再看一个例子。
// 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 = '12345678123456789';
// Retrieving key for the cipher object
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 updated string value with new data
let updatedValue = cipher.update('Some new text data', 'utf8', 'hex');
//Adding the old value and updated value
updatedValue += cipher.final('hex');
// Printing the result...
console.log("Updated String:- " + updatedValue);
});输出
C:\home
ode>> node cipherUpdate.js Updated String:- 91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074
广告
数据结构
网络技术
关系型数据库 RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP