-- Lua-hashings @89879fe79b6f3dc495c607494126ec9c3912b8e9 | /hashings/keccak.lua | https://github.com/user-none/lua-hashings | License: MIT License | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (c) 2016 John Schember
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local a=require("string")local b=require("nums.uintb").u64;local c={}local d={__metatable={},__index=c}local e={{5,2},{1,3},{2,4},{3,5},{4,1}}local f={11,8,12,18,19,4,6,17,9,22,25,5,16,24,20,14,13,3,21,15,23,10,7,2}local g={1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44}local h={{2,3},{3,4},{4,5},{5,1},{1,2},{7,8},{8,9},{9,10},{10,6},{6,7},{12,13},{13,14},{14,15},{15,11},{11,12},{17,18},{18,19},{19,20},{20,16},{16,17},{22,23},{23,24},{24,25},{25,21},{21,22}}local i={b("0x1"),b("0x8082"),b("0x800000000000808A"),b("0x8000000080008000"),b("0x808B"),b("0x80000001"),b("0x8000000080008081"),b("0x8000000000008009"),b("0x8A"),b("0x88"),b("0x80008009"),b("0x8000000A"),b("0x8000808B"),b("0x800000000000008B"),b("0x8000000000008089"),b("0x8000000000008003"),b("0x8000000000008002"),b("0x8000000000000080"),b("0x800A"),b("0x800000008000000A"),b("0x8000000080008081"),b("0x8000000000008080"),b("0x80000001"),b("0x8000000080008008")}local function j(k,l)return k<<l|(k>>64-l)end;local function m(n)local o;local p;for q=1,n._block_size,8 do o=q//8+1;p=q//8*8+1;n._s[o]=n._s[o]~(b(a.byte(n._data,p))|(b(a.byte(n._data,p+1))<<8)|(b(a.byte(n._data,p+2))<<16)|(b(a.byte(n._data,p+3))<<24)|(b(a.byte(n._data,p+4))<<32)|(b(a.byte(n._data,p+5))<<40)|(b(a.byte(n._data,p+6))<<48)|(b(a.byte(n._data,p+7))<<56))end;n._data=n._data:sub(n._block_size+1)end;local function r(s)local t={}local u;for q=1,5 do t[q]=s[q]~s[q+5]~s[q+10]~s[q+15]~s[q+20]end;for q=1,5 do u=t[e[q][1]]~j(t[e[q][2]],1)for o=q,25,5 do s[o]=s[o]~u end end end;local function v(s)local w;local u;u=s[2]:copy()for q=1,24 do w=s[f[q]]s[f[q]]=j(u,g[q])u=w end end;local function x(s)local t={}for q=1,25 do t[q]=s[q]:copy()end;for q=1,25 do s[q]=s[q]~(~t[h[q][1]]&t[h[q][2]])end end;local function y(s,l)s[1]=s[1]~i[l]end;function c:new(z,A,B)if self~=c then return nil,"First argument must be self"end;if z>200 then return nil,"Invalid block size"end;local C=setmetatable({},d)C._block_size=z;C._digest_size=A;C._data=""C._s={}for q=1,25 do C._s[q]=b(0)end;if B~=nil then C:update(B)end;return C end;setmetatable(c,{__call=c.new})function c:copy()local C=setmetatable({},d)C._block_size=self._block_size;C._digest_size=self._digest_size;C._data=self._data;C._s={}for q=1,25 do C._s[q]=self._s[q]:copy()end;return C end;function c:update(B)if B==nil then B=""end;B=tostring(B)self._data=self._data..B;while#self._data>=self._block_size do m(self)for q=1,24 do r(self._s)v(self._s)x(self._s)y(self._s,q)end end end;function c:digest()local D;local B;local E={}D=self:copy()if#D._data==D._block_size-1 then B=a.char(0x06|0x80)else B=a.char(0x06)..a.rep(a.char(0),D._block_size-#D._data-2)..a.char(0x80)end;D:update(B)for q=1,D._digest_size//8 do E[q]=D._s[q]:swape():asbytestring()end;return table.concat(E)end;function c:hexdigest()local F;local E={}F=self:digest()for q=1,#F do E[q]=a.format("%02X",a.byte(F,q))end;return table.concat(E)end;return c
