使用node加密解密数据,创建Hash/HMAC,并生成签名与验证签名

1.数据加密与解密

主要使用了node的crypto模块的Cipher与Decipher类来加密解密,加密与解密的方法主要有以下几种:

1.1 使用Cipher与Decipher对象作为流来加密解密

// 加载 crypto 模块
const crypto = require('crypto')
// 解密密钥
const secret = '123456'
// 加密与解密算法 可用 crypto.getCiphers() 方法查看算法各类
const Algorithm = 'aes192'
// 加密
const cipher = crypto.createCipher(Algorithm, secret)
// 加密后的数据
let encryted = ''
// 订阅 readable 事件
cipher.on('readable', () => {
  // 使用 read 方法读取数据
  let data = cipher.read()
  if (data) {
    // 生成数据
    encryted += data.toString('hex')
  }
})
// 订阅 end 事件打印加密后的数据
cipher.on('end', () => {
  console.log(encryted)
})
// 写入加密数据
cipher.write('Hello World!', 'utf8')
cipher.end()

// 解密数据
const decipher = crypto.createDecipher(Algorithm, secret)
// 解密后的数据
let decryted = ''
// 订阅readable事件读取加密数据并生成解密后的数据
decipher.on('readable', () => {
  let data = decipher.read()
  if (data) {
    decryted += data
  }
})
// 订阅 end 事件打印解密后的数据
decipher.on('end', () => {
  console.log(decryted)
})
// 写入要解密的数据,与设置密数据的编码
decipher.write(encryted, 'hex')
decipher.end()

1.2 使用Cipher与Decipher的update方法与final方法来加密解密

// 加载 crypto 模块
const crypto = require('crypto')
// 解密密钥
const secret = '123456'
// 加密与解密算法 可用 crypto.getCiphers() 方法查看算法各类
const Algorithm = 'aes192'
// 加密
const cipher = crypto.createCipher(Algorithm, secret)
// 更新加密数据
let crypted = cipher.update('Hello World!', 'utf8', 'hex')
// 生成加密数据
crypted += cipher.final('hex')
// 打印加密数据
console.log(crypted)

// 解密
const decipher = crypto.createDecipher(Algorithm, secret)
// 更新解密数据
let decrypted = decipher.update(crypted, 'hex', 'utf8')
// 生成解密数据
decrypted += decipher.final('utf8')
// 打印解密数据
console.log(decrypted)

1.3 使用Cipher与管道流来加密解密文件

// 加载 crypto 模块
const crypto = require('crypto')
// 加载 fs 模块
const fs = require('fs')
// 解密密钥
const secret = '123456'
// 加密与解密算法 可用 crypto.getCiphers() 方法查看算法各类
const Algorithm = 'aes192'
// 创建加密实例
const cipher = crypto.createCipher(Algorithm, secret)
// 创建输入流 input.txt 为要加密的文件
const read = fs.createReadStream('input.txt')
// 创建输出流 encrypted.xt 加密后的文件
const write = fs.createWriteStream('encrypted.txt')
// 通过管道流生成加密文件
read.pipe(cipher).pipe(write)
// 创建实例解密文件
const decipher = crypto.createDecipher(Algorithm, secret)
// 创建输入流 encrypted.txt 为要解密的文件
const input = fs.createReadStream('encrypted.txt')
// 创建输出流 decrypted.txt 为解密后的文件
const ouput = fs.createWriteStream('decrypted.txt')
// 通过管道流生成解密文件
input.pipe(decipher).pipe(ouput)

2.创建Hash与Hmac摘要

2.1 创建Hash

// 加载 crypto 模块
const crypto = require('crypto')
// 创建 Hash 的算法, 可通过crypt.getHashes() 方法获取算法各类
const Algorithm = 'md5'
// 创建 hash 实例
const hash = crypto.createHash(Algorithm)
// 使用update方法更新要加密的数据
hash.update('Hello Wrold!')
// 生成加密后的数据, 以 hex 方式生成
let digest = hash.digest('hex')
// 打印生成结果
console.log(digest)

2.1 创建Hmac

// 加载 crypto 模块
const crypto = require('crypto')
// 创建摘要的算法
const Algorithm = 'md5'
// 密钥
const secret = '123456'
// 创建 hmac 实例
const hmac = crypto.createHmac(Algorithm, secret)
// 使用 update 方法更新加密数据
hmac.update('Hello World!')
// 使用 digest 方法生成加密数据
const digest = hmac.digest('hex')
// 打印加密后数据
console.log(digest)

3. 签名生成与验证签名

签名主要作用如下:

  1. 确认信息来源于特定的主体。
  2. 确认信息完整、未被篡改。

签名的验证主要经两个过程,发送方生成签名,接收方验证签名, 需要使命openssl生成公钥与私钥,私钥用来生成签名,公钥用来验证签名数据

私钥生成:openssl genrsa  -out private_key.pem 1024

公钥生成: openssl req -key private_key.pem -new -x509 -out public_key.pem

// 加载 crypto 模块
const crypto = require('crypto')
// 加载 fs 模块
const fs = require('fs')
// 读取私钥
const privateKey = fs.readFileSync('private_key.pem').toString()
// 读取公钥
const publicKey = fs.readFileSync('public_key.pem').toString()
// 验证的数据
const data = 'Hello World!'
// 签名算法
const Algorithm = 'SHA256'


// 创建签名
const sign = crypto.createSign(Algorithm)
// 使用 update 方法更新数据
sign.update(data)
// 生成签名 以 hex 格式输入数据
const sig = sign.sign(privateKey, 'hex')

// 验证签名
const verify = crypto.createVerify(Algorithm)
// 使用 updata 方法更新数据
verify.update(data)
// 验证签名的数据是否正确
const result = verify.verify(publicKey, sig, 'hex')
// 打印签名验证结果
console.log(result)

你可能感兴趣的:(前端学习,node)