【加解密】bcryptjs | CryptoJS | JSEncrypt | node-rsa 加密| 解密 | RSA | ASE | MD5

npm i bcryptjs 
import bcryptjs from 'bcryptjs'//不可逆加密 bcryptjs 

/**
 * 不可逆加密 ,一旦加密不可解密,只可比对
 * @param {String} txt 加密、比对的文本
 * @param {String} hash 加密的哈希
 * @param {String} type default:jia, [jia 加密 ,bi 比对密码] 操作类型
 * @returns {[String|Boolear]} 返回值
 */
const cyj = (txt, hash, type = 'jia') => {
    let res = null

    // 比对
    if (type === 'bi') {
        if (!txt || !hash) {
            throw '解密参数错误或缺少参数'
        }
        res = bcryptjs.compareSync(txt, hash)
    } else {
        if (!txt) {
            throw '加密请传入明文'
        }
        // 加密
        res = bcryptjs.hashSync(txt)
    }

    return res
}

export default cyj

 

https://blog.csdn.net/qq_43614372/article/details/130867681

posted @ 2023-11-13 17:27  小小强学习网  阅读(152)  评论(0)    收藏  举报