哈希消息认证码HmacSHA256-javascript版

  • 流程
    向后端ajax申请时进行加密,加密是通过一些规则对data加密,调用tokentest方法需要做三个事情
    1.生成时间戳 timestamp
    2.生成随机字符串 nonceStr
    3.通过CryptoJS.HmacSHA256生成加密签名 signature,密要就是nonceStr
let hash = CryptoJS.HmacSHA256(根据一定顺序从系排序后获取的data的键值组成的数据字符串, nonceStr);
let hashInBase64 = CryptoJS.enc.Hex.stringify(hash);

// 生成随机字符串
    function generateMixed() {
        var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

        var res = "";
        for(var i = 0; i < 16 ; i ++) {
            var id = Math.ceil(Math.random()*35);
            res += chars[id];
            }
            return res;
            
        }


    function tokentest(data){
        // 时间戳
        let timestamp =  new Date().getTime();
        // 随即字符串
        let nonceStr  = generateMixed();

        data.timestamp = timestamp;
        data.nonceStr= nonceStr;

        let sorted = {}; //存放排序后的对象
        let newstr='';
        
        // 前后端交互默认字段排序
         Object.keys(data).sort().forEach(item=>{
            sorted[item]=data[item];
        })

        // 获取排序后的属性值
       Object.values(sorted).forEach(item=>{
                newstr+=item;
       });


        // 生成aes加密签名
        let hash = CryptoJS.HmacSHA256(newstr, nonceStr);
        let hashInBase64 = CryptoJS.enc.Hex.stringify(hash);
        // let hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
        // let hashInBase64 = CryptoJS.enc.Utf8.stringify(hash);
        // let hashInBase64 = CryptoJS.enc.Latin1.stringify(hash);
        data.signature = hashInBase64;

        return data;

    }


  function submits() {
    var data = {
        'submit':00001
        ,'submit_s':0001001
        ,'tel':188888888
        ,'s_id':3
        ,'g_id':5,




        // 'tel': 188888888,
        // 'submit': 00001,
        // 'submit_s': 0001001,
        // 'code':88889,
        // 'content':'我想xxx'
        // 'timestamp': xxx,
        // 'nonceStr': 'xxx',
        // 'signature': '43d00401cca4a588xxxxxxxxxxxxxxxxxxxxxxxxxxx',
    }
    

        $.ajax(
            {
                url: /submit/,
                type: 'POST',
                dataType: 'json',
                data:tokentest(data),
        
                cache: false,
                timeout: 20000,
                error: function() {layer.msg('系统错误');},
                success: function(json)
                {
                    console.log(json,'hhh');
                }
            });
    }
//引入的插件文件
 <script src="cryptojs/core.js"></script>
    <script src="cryptojs/hmac.js"></script>
    <script src="cryptojs/sha256.js"></script>
    <script src="cryptojs/hmac-sha256.js"></script>
    <script src="cryptojs/enc-base64.js"></script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值