鐵人賽 Day 16 Node.js 如何生成 MD5、SHA 雜湊值

文章目錄
本系列文章所討論的 JavaScript 資安與逆向工程技術,旨在分享知識、探討防禦之道,並促進技術交流。
所有內容僅供學術研究與學習,請勿用於任何非法或不道德的行為。
讀者應對自己的行為負完全責任。尊重法律與道德規範是所有技術人員應共同遵守的準則。
在前面文章我們已經介紹過 MD5 與 SHA 今天直接實戰使用 CryptoJS 來運算雜湊值。
這個套件不僅能在 Node.js 執行,也能用於瀏覽器端是常見的跨平台加密工具。
安裝CryptoJS
在開始之前需要先安裝crypto-js
npm install crypto-js
使用 CryptoJS 進行 MD5 雜湊
生成 MD5 雜湊值
const CryptoJS = require('crypto-js');
// 待處理的字串
const message = "Hello, world!";
// 生成 MD5 雜湊值
const md5Hash = CryptoJS.MD5(message).toString();
console.log(`原始字串: ${message}`);
console.log(`MD5 雜湊值: ${md5Hash}`);
執行這段程式碼會得到一個32個字元長的MD5雜湊字串。
使用 CryptoJS 進行 SHA 雜湊
生成 SHA1 雜湊值
const CryptoJS = require('crypto-js');
const message = "Hello, world!";
// 生成 SHA1 雜湊值
const sha1Hash = CryptoJS.SHA1(message).toString();
console.log(`原始字串: ${message}`);
console.log(`SHA1 雜湊值: ${sha1Hash}`);
生成 SHA256 雜湊值
const CryptoJS = require('crypto-js');
const message = "Hello, world!";
// 生成 SHA256 雜湊值
const sha256Hash = CryptoJS.SHA256(message).toString();
console.log(`原始字串: ${message}`);
console.log(`SHA256 雜湊值: ${sha256Hash}`);
生成 SHA512 雜湊值
const CryptoJS = require('crypto-js');
const message = "Hello, world!";
// 生成 SHA512 雜湊值
const sha512Hash = CryptoJS.SHA512(message).toString();
console.log(`原始字串: ${message}`);
console.log(`SHA512 雜湊值: ${sha512Hash}`);