{"id":1223,"date":"2009-06-02T16:41:08","date_gmt":"2009-06-02T07:41:08","guid":{"rendered":"https:\/\/regex.info\/blog\/lua\/sha1"},"modified":"2009-06-02T16:41:08","modified_gmt":"2009-06-02T07:41:08","slug":"sha1","status":"publish","type":"page","link":"https:\/\/regex.info\/blog\/lua\/sha1","title":{"rendered":"SHA-1 and HMAC-SHA1 Routines in Pure Lua"},"content":{"rendered":"\n\n<div style='float:right; border:solid 1px #88F; margin: 0 0 20px 2em; padding: 15px 20px; background-color: #922'>\n<a href='\/blog\/lua'>More Lua code<\/a>\n<\/div>\n\n\n\n<p>I've coded up\n<a href='http:\/\/en.wikipedia.org\/wiki\/SHA_hash_functions#SHA-0_and_SHA-1' class='quiet'>SHA-1 Secure Hash<\/a> computation\nand\n<a href='http:\/\/en.wikipedia.org\/wiki\/HMAC-SHA1' class='quiet'>HMAC-SHA1<\/a> secure signature computation in pure <a class='quiet' href='http:\/\/www.lua.org\/'>Lua<\/a> and thought I'd share them in case anyone else would find them useful.<\/p>\n\n<div style='margin: 0 auto; width:30%; text-align:center; padding: 10px 0' class='bg-A'><a href='\/code\/sha1.lua' class='quiet'>Download <b>sha1.lua<\/b><\/a>\n<br\/>Version 1: May 28, 2009\n<\/div>\n\n<p>Lua is a pathetic, horrid, turd of a language, and I associate with it only because it's the language that plugins for Adobe\nLightroom Classic must be written in (and I've written <a href='\/blog\/lightroom-goodies' class='quiet'>quite a\nfew plugins for Adobe Lightroom<\/a>).<\/p>\n\n<p>As you might imagine, the <a class='quiet' href='http:\/\/www.itl.nist.gov\/fipspubs\/fip180-1.htm'>SHA-1 algorithm<\/a> and the\nHMAC-SHA1 algorithm each involve a lot of integer bit fiddling.... shifts, <b>xor<\/b>, <b>and<\/b> and <b>or<\/b> galore &ndash;\nfeatures not supported by Lua. Heck, Lua doesn't even have integers! So while coding this up, I felt as if I were chiseling NAND\ngates out of rough blocks of silicon.<\/p>\n\n<p>The result is not super fast &ndash; the SHA-1 computation on a 10k-byte message takes about 2 seconds on a circa-2008\nmid-level server &ndash; but it should be plenty adequate for short messages, such as is often needed during authentication\nhandshaking. I use these routines to good effect, for example, in providing Twitter support for some of my plugins.<\/p>\n\n<p>(Update: see <a href='#comment-41832'>this comment below<\/a> for a different routine that trades a bit of up-front\ncache-building to allow for a claimed 10x speed improvement. If your SHA-1 needs are more demanding than mine, that library might\nbe much preferable to my library.)<\/p>\n\n<p>Enjoy.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>I've coded up SHA-1 Secure Hash computation and HMAC-SHA1 secure signature computation in pure Lua and thought I'd share them in case anyone else would find them useful.<\/p> <p>Lua is a pathetic, horrid, turd of a language, and I associate with it only because it's the language that plugins for Adobe Lightroom Classic must be written in (and I've written quite a few plugins for Adobe Lightroom).<\/p> <p>As you might imagine, the SHA-1 algorithm and the HMAC-SHA1 algorithm each involve a lot of integer bit fiddling.... shifts, <b>xor<\/b>, <b>and<\/b> and <b>or<\/b> galore &ndash; features not supported by Lua. Heck, Lua [...]","protected":false},"author":1,"featured_media":0,"parent":1222,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages\/1223"}],"collection":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/comments?post=1223"}],"version-history":[{"count":0,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages\/1223\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages\/1222"}],"wp:attachment":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/media?parent=1223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}