• node内置模块——crypoty模块


    crypto模块的作用

    crypto模块的目的是为了提供通用的加密和哈希算法
    用纯JavaScript代码实现这些功能不是不可能,但速度会非常慢。
    Nodejs用C/C++实现这些算法后,通过cypto这 个模块暴露为JavaScript接口,这样用起来方便,运行速度也快。
    如:
    MD5是一种常用的哈希算法,用于给任意数据一个"签名”。 这个签名通常用一个十 六进制的字符串表示。
    MD5是不可逆的,可以用于简单的密码加密,可以用于文件完整性的校验。

    crypto模块的使用

    md5加密——不安全

    const crypto = require("crypto")
    const hash = crypto.createHash("md5")
    // MD5加密
    hash.update("hello world")
    
    // 按16进制方式进行显示
    console.log(hash.digest('hex'))
    // 按base64进制方式进行显示
    // console.log(hash.digest('base64'))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出:

    5eb63bbbe01eeed093cb22bb8f5acdc3
    
    • 1

    md5加密不安全的原因是,MD5加密方法是一样的,所以可以通过‘彩虹表’对照出来加密前的内容。

    sha1加密

    const crypto = require("crypto")
    const hash = crypto.createHash("sha1")
    
    hash.update("hello world")
    
    // 按16进制方式进行显示
    console.log(hash.digest('hex'))
    // 按base64进制方式进行显示
    // console.log(hash.digest('base64'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:

    2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
    
    • 1

    Hmac加密

    比md5多一个密钥。
    只要密钥发生了变化,那么同样的输入数据也会得到不同的签名,因此,可以把Hmac理解为用随机数"增强"的哈希算法。
    const hash = crypto.createHmac("加密算法","密钥值")

    • MD5
    const crypto = require("crypto")
    
    const hash = crypto.createHmac("md5", "yang")
    
    hash.update("hello world")
    
    // 按16进制方式进行显示
    console.log(hash.digest('hex'))
    // 按base64进制方式进行显示
    // console.log(hash.digest('base64'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出:

    1d857df71385733edd6155547b70417c
    
    • 1
    • sha1
    const crypto = require("crypto")
    
    const hash = crypto.createHmac("sha1", "yang")
    
    hash.update("hello world")
    
    // 按16进制方式进行显示
    console.log(hash.digest('hex'))
    // 按base64进制方式进行显示
    // console.log(hash.digest('base64'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出:

    048e6909d740f4365552ccb3e4b34891d247274e
    
    • 1
    • sha256
    const crypto = require("crypto")
    
    const hash = crypto.createHmac("sha256", "yang")
    
    hash.update("hello world")
    
    // 按16进制方式进行显示
    console.log(hash.digest('hex'))
    // 按base64进制方式进行显示
    // console.log(hash.digest('base64'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    输出:

    eb2cbc1dfd99d6af581d634559594bcff31b40c228a9711525d445ee20858e28
    
    • 1

    AES-对称加密算法

    AES是一种常用的对称加密算法,加解解密都用同一个密钥。即该加密方法可以解密成原数据。
    crypto模块提供了AES支持,但是需要自己封装好函数,便于使用。

    const crypto = require("crypto")
    
    // 加密
    function encrypt(key,iv,data) {
        // crypto.createCipheriv("加密算法", key, iv)
        let dep = crypto.createCipheriv("aes-128-cbc", key, iv)
        // dep.update(数据,输入数据的编码格式,输出数据的编码格式)
        // binary是二进制
        return dep.update(data,'binary','hex')+dep.final("hex")
    }
    
    // 解密
    function decrypto(key, iv, cryp) {
        // 转化为Buffer对象,并转换成2进制
        cryp = Buffer.from(cryp, 'hex').toString("binary")
        let dep = crypto.createDecipheriv("aes-128-cbc", key, iv)
        return dep.update(cryp,'binary','utf-8')+dep.final("utf8")
    }
    
    // 128/8 = 16,(加密算法使用的是128位)所以key和iv设置成16位
    let key = "abcdefghijklmnop"
    let iv = "abcdef0123456789"
    
    let data = "helloworld"
    
    // 加密
    let cryp = encrypt(key, iv, data)
    console.log("加密结果:",cryp)
    
    let decryp = decrypto(key, iv, cryp)
    console.log("解密结果:",decryp)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    输出:

    加密结果: af85babb51afffa5b879c5b2506bb69f
    解密结果: helloworld
    
    • 1
    • 2
  • 相关阅读:
    PagerDuty帮助CTC改变远程世界的运营
    Excel中如何用公式列出包含特定文本的所有单元格?
    android源码添加adb host支持
    记一次老商家端应用内存突然飚高原因分析
    鸭绒和鹅绒的区别RDS人道羽绒标准
    Curve 文件系统的重要特点之一就是适用于海量文件存储
    基于CC2530 E18-MS1-PCB Zigbee DIY作品
    希望所有计算机专业同学都知道这些老师
    【面试】对CSS预处理器的理解及与原生CSS的区别
    前端如何使用微信支付
  • 原文地址:https://blog.csdn.net/mantou_riji/article/details/125632840