{"id":536354,"date":"2021-09-20T07:00:42","date_gmt":"2021-09-19T23:00:42","guid":{"rendered":"http:\/\/4563.org\/?p=536354"},"modified":"2021-09-20T07:00:42","modified_gmt":"2021-09-19T23:00:42","slug":"%e5%85%b3%e4%ba%8esha-256-hmac-%e5%8a%a0%e5%af%86%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=536354","title":{"rendered":"\u5173\u4e8eSHA-256 HMAC \u52a0\u5bc6\u7684\u95ee\u9898"},"content":{"rendered":"\n<p>  \t\t\t\t\t<strong>\u658c\u658c<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3>\u5173\u4e8eSHA-256 HMAC \u52a0\u5bc6\u7684\u95ee\u9898<\/h3>\n<p>  \t\t\u6bd4\u5982key\u662f111<br \/>  \u5185\u5bb9\u662fhttps:\/\/imagedelivery.net\/SzNV43bIOhJnWJ5Iz6Cvhg\/932c3295-9ef0-43c0-45cf-082dd1d6ed01\/public?exp=1632165833<br \/>  \u52a0\u5bc6\u540e\u5f97\u5230\u7684\u503c\u662f1d160cf8c35752b652a3d5fc542634560e7a990091ea1073cd6fb42822f5a349\u3002\u5c31\u662f\u6b63\u786e\u7684<\/p>\n<p>  \u4f46\u662f\u6211c# \u5f97\u5230\u7684\u662f4090da18a78252c31e4e43dbb89ec78e79a8d7db3199285ac1204c7043819eab<br \/>  \u4e0b\u9762\u4ee3\u7801\u6709\u4ec0\u4e48\u95ee\u9898\u5417\u3002<br \/>  public static string Encrypt(string message, string secret)<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;{<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;var encoding = new System.Text.UTF8Encoding();<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;byte[] keyByte = encoding.GetBytes(secret);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;byte[] messageBytes = encoding.GetBytes(message);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;using (var hmacsha256 = new HMACSHA256(keyByte))<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; StringBuilder builder = new StringBuilder();<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for (int i = 0; i &lt; hashmessage.Length; i++)<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;builder.Append(hashmessage.ToString(&quot;x2&quot;));<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; return builder.ToString();<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;}\t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>sdqu<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\tHMAC SHA256 \u4f60\u6ca1\u7b97\u9519<br \/>  \u7136\u800c\uff0c\u4f60\u5e94\u8be5\u662f\u6ca1\u5f04\u5bf9\u5177\u4f53\u7b97\u6cd5\u3002<\/p>\n<p>  https:\/\/imgpp.com\/images\/2021\/09\/19\/16452651862ae767c.png\t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>\u658c\u658c<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\tsdqu \u5927\u4f6c\u6709\u8bdd\u8bf4 : 2021-9-20 03:38<br \/>  HMAC SHA256 \u4f60\u6ca1\u7b97\u9519<br \/>  \u7136\u800c\uff0c\u4f60\u5e94\u8be5\u662f\u6ca1\u5f04\u5bf9\u5177\u4f53\u7b97\u6cd5\u3002<\/p>\n<p> \u554a\u3002\u5927\u4f6c\u3002\u80fd\u7ed9\u6307\u70b9\u4e0b\u4e48\u3002\u53ef\u80fd\u5173\u952e\u70b9\u5728\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u4e0d\u660e\u767d\u4f5c\u7528\u3002<br \/>  const key = await crypto.subtle.importKey(<br \/>  &nbsp; &nbsp; &quot;raw&quot;,<br \/>  &nbsp; &nbsp; secretKeyData,<br \/>  &nbsp; &nbsp; { name: &quot;HMAC&quot;, hash: &quot;SHA-256&quot; },<br \/>  &nbsp; &nbsp; false,<br \/>  &nbsp; &nbsp; [&quot;sign&quot;]<br \/>  );<\/p>\n<p>  https:\/\/developers.cloudflare.com\/images\/serving-images\/serving-private-images-using-signed-url-tokens  \t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>sdqu<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\t \u672c\u5e16\u6700\u540e\u7531 sdqu \u4e8e 2021-9-20 04:51 \u7f16\u8f91 <\/p>\n<p> \u6211\u4e0d\u61c2Web Crypto API\uff0c\u6211\u7528\u6211\u4e00\u822c\u7684\u7f16\u7a0b\u7406\u89e3\u6765\u8bf4\uff0c\u629b\u7816\u5f15\u7389\uff0c\u9519\u4e86\u522b\u602a\u6211\u3002<br \/>  \u4f60\u7684\u4ee3\u7801\u6211\u4e0d\u770b\u4e86\uff0c\u6211\u53ea\u8bf4CF\u7684\u4ee3\u7801\u3002<\/p>\n<p>  \u8fd9\u6bb5\u662f\u6839\u636e\u4f60\u7684KEY\u751f\u6210key(\u6ce8\u610f\u5927\u5c0f\u5199\u4e0d\u540c\uff09,\u7b97\u6cd5\u662f HMAC-SHA256\uff0c\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2akey\u662f\u7528\u4e8e\u94fe\u63a5\u7b7e\u540d\u7684\u3002<\/p>\n<p>  const secretKeyData = encoder.encode(KEY);<br \/>  const key = await crypto.subtle.importKey(<br \/>  &nbsp; &nbsp; &quot;raw&quot;,<br \/>  &nbsp; &nbsp; secretKeyData,<br \/>  &nbsp; &nbsp; { name: &quot;HMAC&quot;, hash: &quot;SHA-256&quot; },<br \/>  &nbsp; &nbsp; false,<br \/>  &nbsp; &nbsp; [&quot;sign&quot;]<br \/>  );<\/p>\n<p>  \u4e0b\u9762\u8fd9\u6bb5\uff0c\u53ea\u4f7f\u7528HMAC\u7b97\u6cd5, key\u4e3a\u4e0a\u9762\u751f\u6210\u7684\uff0c\u7136\u540e\u4e3a\u94fe\u63a5\u751f\u6210\u4e86\u7b7e\u540d\u3002\u5176\u5b9e\u6807\u51c6HMAC\u7b97\u6cd5\u91cc\uff0c\u5e94\u8be5\u6709\u4e00\u4e2a\u52a0\u5bc6\u65b9\u5f0f\uff0c\u8fd9\u91cc\u6ca1\u6709\uff0c\u6211\u731c\u662fcrypto api\u6709\u4e2a\u9ed8\u8ba4\u7684\u7b97\u6cd5\uff0c\u6bd4\u5982SHA1\u3002<\/p>\n<p>  const mac = await crypto.subtle.sign(<br \/>  &nbsp; &nbsp; &quot;HMAC&quot;,<br \/>  &nbsp; &nbsp; key,<br \/>  &nbsp; &nbsp; encoder.encode(stringToSign)<\/p>\n<p>  \u6240\u4ee5\uff0c\u4f60\u628aKEY\u76f4\u63a5\u62ff\u6765\u94fe\u63a5\u8fd9\u91cc(\u4e0b\u9762\u8fd9\u6bb5)\u505aHMAC SHA256\u662f\u4e0d\u5bf9\u7684\u3002<\/p>\n<p>  \u603b\u7ed3\uff1a<br \/>  1. \u7528\u4f60\u7684KEY\uff0c\u505aSHA256 HMAC\uff0c\u5f97\u5230\u94fe\u63a5\u52a0\u5bc6\u5bc6\u94a5key<br \/>  2. \u7528key,\u5bf9\u94fe\u63a5\u505aHMAC\u3002  \t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>mmc199<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\t\u8fd9\u4e2a\u658c\u658c\u5c31\u662f\u900a\u5566\t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>XiaoFaye<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\t\u8fd9\u4e48\u5feb\u5c31\u7528\u4e0aCF\u7684\u56fe\u5e8a\u5566\uff1f\u8bf7\u95ee\u4e0b\u5b83\u7684\u56fe\u7247\u53ef\u4ee5\u5206\u7ec4\u5417\uff1f\u6709\u5c42\u7ea7\u7ed3\u6784\u5417\uff1f\t\t\t  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u658c\u658c \u5927\u4f6c\u6709\u8bdd\u8bf4 : \u5173\u4e8eSHA-&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/536354"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=536354"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/536354\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=536354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=536354"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=536354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}