-- Lua-hashings @89879fe79b6f3dc495c607494126ec9c3912b8e9 | /hashings/sha1.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.uintn").u32;local c={}local d={__metatable={},__index=c}c.digest_size=20;c.block_size=64;local function e(f,g)return f<<g|(f>>32-g)end;function c:new(h)if self~=c then return nil,"First argument must be self"end;local i=setmetatable({},d)i._H0=b(0x67452301)i._H1=b(0xEFCDAB89)i._H2=b(0x98BADCFE)i._H3=b(0x10325476)i._H4=b(0xC3D2E1F0)i._len=0;i._data=""if h~=nil then i:update(h)end;return i end;setmetatable(c,{__call=c.new})function c:copy()local i=c:new()i._H0=self._H0:copy()i._H1=self._H1:copy()i._H2=self._H2:copy()i._H3=self._H3:copy()i._H4=self._H4:copy()i._data=self._data;i._len=self._len;return i end;function c:update(h)local j=b(0x5A827999)local k=b(0x6ED9EBA1)local l=b(0x8F1BBCDC)local m=b(0xCA62C1D6)local n;local o;local p;local q;local r;local s;local t;if h==nil then h=""end;h=tostring(h)self._len=self._len+#h;self._data=self._data..h;while#self._data>=64 do n={}for u=1,64,4 do local v=#n+1;n[v]=b(a.byte(self._data,u))<<24;n[v]=n[v]|(b(a.byte(self._data,u+1))<<16)n[v]=n[v]|(b(a.byte(self._data,u+2))<<8)n[v]=n[v]|b(a.byte(self._data,u+3))end;for u=17,80 do n[u]=e(n[u-3]~n[u-8]~n[u-14]~n[u-16],1)end;p=self._H0;q=self._H1;r=self._H2;s=self._H3;t=self._H4;for u=1,20 do o=e(p,5)+q&r|(~q&s)+t+n[u]+j;t=s;s=r;r=e(q,30)q=p;p=o end;for u=21,40 do o=e(p,5)+q~r~s+t+n[u]+k;t=s;s=r;r=e(q,30)q=p;p=o end;for u=41,60 do o=e(p,5)+q&r|(q&s)|(r&s)+t+n[u]+l;t=s;s=r;r=e(q,30)q=p;p=o end;for u=61,80 do o=e(p,5)+q~r~s+t+n[u]+m;t=s;s=r;r=e(q,30)q=p;p=o end;self._H0=self._H0+p;self._H1=self._H1+q;self._H2=self._H2+r;self._H3=self._H3+s;self._H4=self._H4+t;self._data=self._data:sub(65,#self._data)end end;function c:digest()local w;local h;local x=0;local y=0;w=self:copy()y=w._len%64;if y<56 then y=56-y else y=120-y end;x=w._len*8;h=a.char(1<<7)..a.rep(a.char(0),y-1)..a.char(x>>56&0xFF)..a.char(x>>48&0xFF)..a.char(x>>40&0xFF)..a.char(x>>32&0xFF)..a.char(x>>24&0xFF)..a.char(x>>16&0xFF)..a.char(x>>8&0xFF)..a.char(x&0xFF)w:update(h)return w._H0:asbytestring()..w._H1:asbytestring()..w._H2:asbytestring()..w._H3:asbytestring()..w._H4:asbytestring()end;function c:hexdigest()local z;local A={}z=self:digest()for u=1,#z do A[u]=a.format("%02X",a.byte(z,u))end;return table.concat(A)end;return c
