的,用于根据给定的编码格式与receivd数据来更新密码。它是加密模块中的类 Cipher 提供的内置方法之一。如果指定了输入编码,则数据参数是字符串,否则数据参数是缓冲区cipher.update()
cipher.update(data, [inputEncoding], [outputEncoding])
上述参数描述如下 -
data – 它将数据作为输入,传递给更新密码内容。
inputEncoding – 它将输入编码作为参数。可能的输入值为十六进制、base64 等。
outputEncoding – 它将输出编码作为参数。此参数的输入类型是字符串。可能的输入值为十六进制、base64 等。
创建一个具有名称的文件 -cipherUpdate.js并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 -
node cipherUpdate.js
密码更新.js
// 演示 cipher.final() 方法使用的示例 // 导入加密模块 const crypto = require('crypto'); // 初始化 AES 算法 const algorithm = 'aes-192-cbc'; // 初始化用于生成密钥的密码 const password = '12345678123456789'; // 检索密码对象的密钥 const key = crypto.scryptSync(password, 'old data', 24); // 初始化静态iv const iv = Buffer.alloc(16, 0); // 初始化密码对象以获取密码 const cipher = crypto.createCipheriv(algorithm, key, iv); //使用新数据获取更新的字符串值 let updatedValue = cipher.update('Welcome to tutorials point', 'utf8', 'hex'); //添加旧值和更新值 updatedValue += cipher.final('hex'); // 打印结果... console.log("Updated String:- " + updatedValue);输出结果
C:\home\node>> node cipherUpdate.js Updated String:- a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a
让我们再看一个例子。
// 演示 cipher.final() 方法使用的示例 // 导入加密模块 const crypto = require('crypto'); // 初始化 AES 算法 const algorithm = 'aes-192-cbc'; // 初始化用于生成密钥的密码 const password = '12345678123456789'; // 检索密码对象的密钥 crypto.scrypt(password, 'salt', 24, { N: 512 }, (err, key) => { if (err) throw err; // 初始化静态iv const iv = Buffer.alloc(16, 0); // 初始化密码对象以获取密码 const cipher = crypto.createCipheriv(algorithm, key, iv); //使用新数据获取更新的字符串值 let updatedValue = cipher.update('Some new text data', 'utf8', 'hex'); //添加旧值和更新值 updatedValue += cipher.final('hex'); // 打印结果... console.log("Updated String:- " + updatedValue); });输出结果
C:\home\node>> node cipherUpdate.js Updated String:- 91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074