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摘要的工具。
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加密示例:
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);
