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);