เมธอด cipher.update() อัปเดต cipher ด้วยข้อมูลที่ได้รับตาม encoding ที่กำหนด เป็นเมธอดในตัวของคลาส Cipher ในโมดูล crypto หากระบุ inputEncoding ข้อมูลจะเป็น string มิฉะนั้นเป็น Buffer
ไวยากรณ์
cipher.update(data, [inputEncoding], [outputEncoding])
พารามิเตอร์
- data – ข้อมูลที่จะอัปเดต cipher
- inputEncoding – encoding ของข้อมูลเข้า (เช่น
utf8,hex,base64) - outputEncoding – encoding ของผลลัพธ์ (เช่น
hex,base64)
ตัวอย่าง: ใช้ scryptSync (synchronous)
const crypto = require('crypto');
const algorithm = 'aes-192-cbc';
const password = '12345678123456789';
const key = crypto.scryptSync(password, 'old data', 24);
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);
ผลลัพธ์
Updated String:- a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a
ตัวอย่าง: ใช้ scrypt (asynchronous)
crypto.scrypt(password, 'salt', 24, { N: 512 }, (err, key) => {
if (err) throw err;
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);
});
ผลลัพธ์
Updated String:- 91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074