Dark mode
Crypto Module
เหตุผลที่ควรเรียนรู้ crypto
crypto คือเครื่องมือสำคัญสำหรับงานที่เกี่ยวกับความปลอดภัย เช่น เข้ารหัส, hash password, สุ่ม token มือใหม่ควรรู้จัก crypto เพื่อป้องกันข้อมูลรั่วไหลและสร้างระบบที่ปลอดภัย
- ใช้ crypto สร้าง password hash, สุ่ม token, เข้ารหัสข้อมูล
- ทุกระบบที่ต้องการความปลอดภัยจะขาด crypto ไม่ได้
- Node.js มี crypto ในตัว ใช้งานง่ายและปลอดภัย
แนะนำ Crypto module
crypto
เป็น built-in module สำหรับเข้ารหัส, ถอดรหัส, hash, และสุ่มข้อมูลอย่างปลอดภัยใน Node.js
ฟังก์ชันหลักที่ใช้บ่อย
crypto.createHash(algorithm)
สร้าง hash ของข้อมูล
js
const crypto = require("crypto");
const hash = crypto.createHash("sha256").update("hello").digest("hex");
console.log(hash);
crypto.randomBytes(size)
สร้างข้อมูลสุ่มที่ปลอดภัย
js
const random = crypto.randomBytes(16).toString("hex");
console.log(random);
crypto.createCipheriv(algorithm, key, iv)
เข้ารหัสข้อมูล (advanced)
crypto.createDecipheriv(algorithm, key, iv)
ถอดรหัสข้อมูล (advanced)
กรณีใช้งานจริง
- สร้าง password hash
- สุ่ม token สำหรับ reset password
- เข้ารหัส/ถอดรหัสข้อมูลสำคัญ