{"id":2258,"date":"2023-04-16T19:23:55","date_gmt":"2023-04-16T17:23:55","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=2258"},"modified":"2023-04-16T19:25:21","modified_gmt":"2023-04-16T17:25:21","slug":"calculating-hmac-sha-1-in-the-browser","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2023\/04\/16\/calculating-hmac-sha-1-in-the-browser\/","title":{"rendered":"Calculating HMAC SHA-1 in the Browser"},"content":{"rendered":"<p>If you&#8217;re looking for the equivalent of <code>hash_hmac('sha1', 'string', 'secret');<\/code> in JavaScript, then here you go:<\/p>\n<pre class=\"brush:js\">\r\nasync function hmac_sha1 (str, secret) {\r\n  \/\/ see https:\/\/stackoverflow.com\/a\/47332317\/1134119\r\n  let enc = new TextEncoder(\"utf-8\");\r\n  let key = await window.crypto.subtle.importKey(\r\n    \"raw\", \/\/ raw format of the key - should be Uint8Array\r\n    enc.encode(secret),\r\n    { \/\/ algorithm details\r\n      name: \"HMAC\",\r\n      hash: {name: \"SHA-1\"}\r\n    },\r\n    false, \/\/ export = false\r\n    [\"sign\", \"verify\"] \/\/ what this key can do\r\n  );\r\n  let signature = await window.crypto.subtle.sign(\r\n    \"HMAC\",\r\n    key,\r\n    enc.encode(str)\r\n  );\r\n  let b = new Uint8Array(signature);\r\n  return Array.prototype.map.call(b, x => x.toString(16).padStart(2, '0')).join(\"\");\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re looking for the equivalent of hash_hmac(&#8216;sha1&#8217;, &#8216;string&#8217;, &#8216;secret&#8217;); in JavaScript, then here you go: async function hmac_sha1 (str, secret) { \/\/ see https:\/\/stackoverflow.com\/a\/47332317\/1134119 let enc = new TextEncoder(&#8220;utf-8&#8221;); let key = await window.crypto.subtle.importKey( &#8220;raw&#8221;, \/\/ raw format of the key &#8211; should be Uint8Array enc.encode(secret), { \/\/ algorithm details name: &#8220;HMAC&#8221;, hash: {name: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[15,170,20,33],"tags":[],"class_list":["post-2258","post","type-post","status-publish","format-standard","hentry","category-astuce","category-english","category-niveau-expert","category-programmation"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/2258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2258"}],"version-history":[{"count":3,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/2258\/revisions"}],"predecessor-version":[{"id":2261,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/2258\/revisions\/2261"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}