JavaScript实现HmacSHA256摘要

引入库

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/hmac-sha256.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/enc-base64.min.js"></script>
</head>

版本查询:https://cdnjs.com/libraries

HmacSHA256摘要

<script>
$("#signature").click(function() {
    var name = $("#name").val()
    var email = $("#email").val()

    var text = "";
    if (name !== undefined && name != null && name.length > 0) {
        text += "name" + name
    }
    if (email !== undefined && email != null && email.length > 0) {
        text += "email" + email
    }
    console.log(text)
    var hash = CryptoJS.HmacSHA256(text, "secret");
    var signature = base64toHEX(CryptoJS.enc.Base64.stringify(hash)).toLowerCase();
    console.log(signature)

    $("#sign").val(signature)
});

function base64toHEX(base64) {
  var raw = atob(base64);
  var HEX = '';
  for ( i = 0; i < raw.length; i++ ) {
    var _hex = raw.charCodeAt(i).toString(16)
    HEX += (_hex.length==2 ? _hex : '0'+_hex);
  }
  return HEX.toUpperCase();
}
</script>

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/25/javascript-hmacsha256/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
JavaScript实现HmacSHA256摘要
引入库 <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script> <……
<<上一篇
下一篇>>
文章目录
关闭
目 录