{"id":672,"date":"2023-02-25T14:09:14","date_gmt":"2023-02-25T06:09:14","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=672"},"modified":"2023-02-26T14:09:46","modified_gmt":"2023-02-26T06:09:46","slug":"javascript-hmacsha256","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/javascript-hmacsha256\/","title":{"rendered":"JavaScript\u5b9e\u73b0HmacSHA256\u6458\u8981"},"content":{"rendered":"<h2>\u5f15\u5165\u5e93<\/h2>\n<pre><code class=\"language-html\">&lt;head&gt;\n  &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/crypto-js\/3.1.9-1\/crypto-js.min.js&quot;&gt;&lt;\/script&gt;\n  &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/crypto-js\/3.1.9-1\/hmac-sha256.min.js&quot;&gt;&lt;\/script&gt;\n  &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/crypto-js\/3.1.9-1\/enc-base64.min.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u7248\u672c\u67e5\u8be2\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/cdnjs.com\/libraries\">https:\/\/cdnjs.com\/libraries<\/a><\/p>\n<h2>HmacSHA256\u6458\u8981<\/h2>\n<pre><code class=\"language-javascript\">&lt;script&gt;\n$(&quot;#signature&quot;).click(function() {\n    var name = $(&quot;#name&quot;).val()\n    var email = $(&quot;#email&quot;).val()\n\n    var text = &quot;&quot;;\n    if (name !== undefined &amp;&amp; name != null &amp;&amp; name.length &gt; 0) {\n        text += &quot;name&quot; + name\n    }\n    if (email !== undefined &amp;&amp; email != null &amp;&amp; email.length &gt; 0) {\n        text += &quot;email&quot; + email\n    }\n    console.log(text)\n    var hash = CryptoJS.HmacSHA256(text, &quot;secret&quot;);\n    var signature = base64toHEX(CryptoJS.enc.Base64.stringify(hash)).toLowerCase();\n    console.log(signature)\n\n    $(&quot;#sign&quot;).val(signature)\n});\n\nfunction base64toHEX(base64) {\n  var raw = atob(base64);\n  var HEX = &#039;&#039;;\n  for ( i = 0; i &lt; raw.length; i++ ) {\n    var _hex = raw.charCodeAt(i).toString(16)\n    HEX += (_hex.length==2 ? _hex : &#039;0&#039;+_hex);\n  }\n  return HEX.toUpperCase();\n}\n&lt;\/script&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u5165\u5e93 &lt;head&gt; &lt;script src=&quot;https:\/\/cdnjs.clo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[190],"class_list":["post-672","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-hmac-sha"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=672"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/672\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}