当前位置: 首页 > 工具软件 > TCrypto > 使用案例 >

node_crypto&zlib

冯风史
2023-12-01

crypto

  • crypto.getCiphers() - 查看Node.js中能够使用的所有加密算法。
  • crypto.getHashes() - 查看Node.js中能够使用的所有散列算法。
  • 私钥的拥有者可以在数据发送前先对该数据进行签名操作,签名过程中将对这段数据进行加密处理。数据的接收者可以通过公钥的使用对该签名进行解密及验证操作,以确保这段数据是私钥的拥有者所发出原始数据且在网络的传输过程中未被修改

散列算法

  • const hash = crypto.createHash(algorithm)
    • algorithm - sha1,md5,sha256,sha512,ripemd160等
  • hash.update(data, [input_encoding])
    • 创建摘要
    • data - 一个Buffer对象或者字符串
    • input_encoding - ‘utf8’, ‘ascii’, ‘binary’
  • hash.digest([encoding])
    • 输出摘要内容,使用了digest之后就不能再向对象中追加摘要内容,即hash这个对象后续不能再被使用
    • encoding - ‘hex’, ‘base64’, ‘binary’

HMAC算法

  • 将散列和密钥结合,阻止对签名完整性的破坏
  • const hmac = crypto.createHmac(algorithm, key)
    • key - 字符串, 指定一个PEM格式的密钥
  • hmac.update(data)
  • hmac.digest([encoding])

公钥加密

  • Cipher类:加密数据
    • const cipher = crypto.createCipher(algorithm, password)
      • algorithm - blowfish, aes-256-cbc等
      • password - 指定加密时所使用的密码,二进制格式的字符串或Buffer对象
    • const cipher = crypto.createCipheriv(algorithm, password, iv)
      • iv - 指定加密时所使用的初始向量,二进制格式的字符串或Buffer对象
    • cipher.update(data, [input_encoding], [output_encoding])
      • input_encoding - ‘utf8’, ‘ascii’, ‘binary’
      • output_encoding - ‘hex’, ‘base64’, ‘binary’
      • 可使用update多次添加需要加密的数据,与hash和hmac不同的是,cipher的update总是返回一个被分块的数据。
        • 如果加密的数据字节数足够创建一个及以上的块,返回被加密的数据
        • 如果不足以创建一个块,加密数据将被缓存在cipher对象中
    • cipher.final([output_encoding])
      • 最终返回加密数据
  • Decipher类:解密数据
    • const decipher = crypto.createDecipher(algorithm, password)
    • const decipher = crypto.createDecipheriv(algorithm, password, iv)
    • decipher.update(data, [input_encoding], [output_encoding])
    • decipher.final([output_encoding])
  • Sign类:生成签名
    • const sign = crypto.createSign(algorithm)
      • algorithm - RSA-SHA256
    • sign.update(data)
    • sign.sign(private_key, [output_format])
      • private_key - 字符串,指定PEM格式的私钥
      • output_format - ‘hex’, ‘base64’, ‘binary’
  • Verify类:验证签名
    • const verify = crypto.createVerify(algorithm)
      • algorithm - RSA-SHA256
    • verify.update(data)
    • verify.verify(object, signature, [signature_format])
      • 返回结果为布尔值
      • object - 字符串,公钥
      • signature - 签名对象

zlib

  • 创建各种用于压缩及解压缩的对象,均为既可用于读取流数据的对象,又可用于写入流数据的对象

    • zlib.createGzip([options]) - 压缩

      • zlib.createGunzip([options]) - 解压
    • zlib.createDeflate([options]) - 压缩

      • zlib.createInflate([options]) - 解压
    • zlib.createDeflateRaw([options]) - 压缩

      • zlib.createInflateRaw([options]) - 解压
    • zlib.createUnzip([options]) - 解压

      • 即可解压Gzip的也可以解压Deflate的
    • options - 指定压缩或解压数据时所使用的选项

      • flush
      • chunkSize
      • windowBits
      • level
      • memLevel
      • strategy
    • 压缩解压缩代码示例

      const zlib = require('zlib')
      const fs = require('fs')
      // 使用Gzip对象压缩文件
      const gzip = zlib.createGzip()
      const inp = fs.createReadStream('test.txt')
      const out = fs.createWriteStream('test.txt.gz')
      inp.pipe(gzip).pipe(out)
      
      // 使用Gunzip对象解压缩文件
      const gunzip = zlib.createGunzip()
      const inp = fs.createReadStream('test.txt.gz')
      const out = fs.createWriteStream('test.txt')
      inp.pipe(gunzip).pipe(out)
      
  • 各种用于压缩或解压缩数据的方法,这些方法不再使用options,使用各种默认选项

    • zlib.gzip(buf, callback)
      • zlib.gunzip(buf, callback)
      • callback - (err, buffer) => {}
        • buffer - 解压或解压缩的结果
    • zlib.deflate(buf, callback)
      • zlib.inflate(buf, callback)
    • zlib.deflateRaw(buf, callback)
      • zlib.inflateRaw(buf, callback)
    • zlib.unzip(buf, callback)a
 类似资料: