nodejs的crypto模塊


1. 散列HASH

Hash類對數據進行散列摘要的工具,使用示例:

const crypto = require('crypto');
const hash = crypto.createHash('sha256');

hash.update('123')
console.log(hash.digest('hex'))

 2. 散列消息身份驗證碼HMAC(Hashed Message Authentication Code)

HMac基本介紹

Hmac類是創建HMAC摘要的工具。

const crypto = require('crypto');
const hmac = crypto.createHmac('sha256','a secret');

hmac.update('some data to hash')
console.log(hmac.digest('hex'))

 3. 加密解密

Cipher類用於加密數據,Decipher類用於解密數據。

AES加密概述

AES加密示例:

const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');

let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM