哈希消息認(rèn)證碼HmacSHA256

  • 流程
    向后端ajax申請(qǐng)時(shí)進(jìn)行加密,加密是通過(guò)一些規(guī)則對(duì)data加密,調(diào)用tokentest方法需要做三個(gè)事情
    1.生成時(shí)間戳 timestamp
    2.生成隨機(jī)字符串 nonceStr
    3.通過(guò)CryptoJS.HmacSHA256生成加密簽名 signature,密要就是nonceStr
let hash = CryptoJS.HmacSHA256(根據(jù)一定順序從系排序后獲取的data的鍵值組成的數(shù)據(jù)字符串, nonceStr);
let hashInBase64 = CryptoJS.enc.Hex.stringify(hash);

// 生成隨機(jī)字符串
    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){
        // 時(shí)間戳
        let timestamp =  new Date().getTime();
        // 隨即字符串
        let nonceStr  = generateMixed();

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

        let sorted = {}; //存放排序后的對(duì)象
        let newstr='';
        
        // 前后端交互默認(rèn)字段排序
         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('系統(tǒng)錯(cuò)誤');},
                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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容