-- Lua-hashings @89879fe79b6f3dc495c607494126ec9c3912b8e9 | /hashings/sha512.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}c.digest_size=64;c.block_size=128;local e={b("0x428A2F98D728AE22"),b("0x7137449123EF65CD"),b("0xB5C0FBCFEC4D3B2F"),b("0xE9B5DBA58189DBBC"),b("0x3956C25BF348B538"),b("0x59F111F1B605D019"),b("0x923F82A4AF194F9B"),b("0xAB1C5ED5DA6D8118"),b("0xD807AA98A3030242"),b("0x12835B0145706FBE"),b("0x243185BE4EE4B28C"),b("0x550C7DC3D5FFB4E2"),b("0x72BE5D74F27B896F"),b("0x80DEB1FE3B1696B1"),b("0x9BDC06A725C71235"),b("0xC19BF174CF692694"),b("0xE49B69C19EF14AD2"),b("0xEFBE4786384F25E3"),b("0x0FC19DC68B8CD5B5"),b("0x240CA1CC77AC9C65"),b("0x2DE92C6F592B0275"),b("0x4A7484AA6EA6E483"),b("0x5CB0A9DCBD41FBD4"),b("0x76F988DA831153B5"),b("0x983E5152EE66DFAB"),b("0xA831C66D2DB43210"),b("0xB00327C898FB213F"),b("0xBF597FC7BEEF0EE4"),b("0xC6E00BF33DA88FC2"),b("0xD5A79147930AA725"),b("0x06CA6351E003826F"),b("0x142929670A0E6E70"),b("0x27B70A8546D22FFC"),b("0x2E1B21385C26C926"),b("0x4D2C6DFC5AC42AED"),b("0x53380D139D95B3DF"),b("0x650A73548BAF63DE"),b("0x766A0ABB3C77B2A8"),b("0x81C2C92E47EDAEE6"),b("0x92722C851482353B"),b("0xA2BFE8A14CF10364"),b("0xA81A664BBC423001"),b("0xC24B8B70D0F89791"),b("0xC76C51A30654BE30"),b("0xD192E819D6EF5218"),b("0xD69906245565A910"),b("0xF40E35855771202A"),b("0x106AA07032BBD1B8"),b("0x19A4C116B8D2D0C8"),b("0x1E376C085141AB53"),b("0x2748774CDF8EEB99"),b("0x34B0BCB5E19B48A8"),b("0x391C0CB3C5C95A63"),b("0x4ED8AA4AE3418ACB"),b("0x5B9CCA4F7763E373"),b("0x682E6FF3D6B2B8A3"),b("0x748F82EE5DEFB2FC"),b("0x78A5636F43172F60"),b("0x84C87814A1F0AB72"),b("0x8CC702081A6439EC"),b("0x90BEFFFA23631E28"),b("0xA4506CEBDE82BDE9"),b("0xBEF9A3F7B2C67915"),b("0xC67178F2E372532B"),b("0xCA273ECEEA26619C"),b("0xD186B8C721C0C207"),b("0xEADA7DD6CDE0EB1E"),b("0xF57D4F7FEE6ED178"),b("0x06F067AA72176FBA"),b("0x0A637DC5A2C898A6"),b("0x113F9804BEF90DAE"),b("0x1B710B35131C471B"),b("0x28DB77F523047D84"),b("0x32CAAB7B40C72493"),b("0x3C9EBE0A15C9BEBC"),b("0x431D67C49C100D4C"),b("0x4CC5D4BECB3E42B6"),b("0x597F299CFC657E2A"),b("0x5FCB6FAB3AD6FAEC"),b("0x6C44198C4A475817")}local function f(g,h)return g>>h|(g<<64-h)end;local function i(g,j,k)return g&j~(~g&k)end;local function l(g,j,k)return g&j~(g&k)~(j&k)end;local function m(g)return f(g,28)~f(g,34)~f(g,39)end;local function n(g)return f(g,14)~f(g,18)~f(g,41)end;local function o(g)return f(g,1)~f(g,8)~(g>>7)end;local function p(g)return f(g,19)~f(g,61)~(g>>6)end;function c:new(q)if self~=c then return nil,"First argument must be self"end;local r=setmetatable({},d)r._H0=b(0x6A09E667F3BCC908)r._H1=b(0xBB67AE8584CAA73B)r._H2=b(0x3C6EF372FE94F82B)r._H3=b(0xA54FF53A5F1D36F1)r._H4=b(0x510E527FADE682D1)r._H5=b(0x9B05688C2B3E6C1F)r._H6=b(0x1F83D9ABFB41BD6B)r._H7=b(0x5BE0CD19137E2179)r._len=0;r._data=""if q~=nil then r:update(q)end;return r end;setmetatable(c,{__call=c.new})function c:copy()local r=c:new()r._H0=self._H0:copy()r._H1=self._H1:copy()r._H2=self._H2:copy()r._H3=self._H3:copy()r._H4=self._H4:copy()r._H5=self._H5:copy()r._H6=self._H6:copy()r._H7=self._H7:copy()r._data=self._data;r._len=self._len;return r end;function c:update(q)local s;local t;local u;local v;local w;local x;local y;local z;local A;local B;local C;if q==nil then q=""end;q=tostring(q)self._len=self._len+#q;self._data=self._data..q;while#self._data>=128 do s={}for D=1,128,8 do local E=#s+1;s[E]=b(a.byte(self._data,D))<<56;s[E]=s[E]|(b(a.byte(self._data,D+1))<<48)s[E]=s[E]|(b(a.byte(self._data,D+2))<<40)s[E]=s[E]|(b(a.byte(self._data,D+3))<<32)s[E]=s[E]|(b(a.byte(self._data,D+4))<<24)s[E]=s[E]|(b(a.byte(self._data,D+5))<<16)s[E]=s[E]|(b(a.byte(self._data,D+6))<<8)s[E]=s[E]|b(a.byte(self._data,D+7))end;self._data=self._data:sub(129,#self._data)for D=17,80 do s[D]=p(s[D-2])+s[D-7]+o(s[D-15])+s[D-16]end;v=self._H0;w=self._H1;x=self._H2;y=self._H3;z=self._H4;A=self._H5;B=self._H6;C=self._H7;for D=1,80 do t=C+n(z)+i(z,A,B)+e[D]+s[D]u=m(v)+l(v,w,x)C=B;B=A;A=z;z=y+t;y=x;x=w;w=v;v=t+u end;self._H0=self._H0+v;self._H1=self._H1+w;self._H2=self._H2+x;self._H3=self._H3+y;self._H4=self._H4+z;self._H5=self._H5+A;self._H6=self._H6+B;self._H7=self._H7+C end end;function c:digest()local F;local q;local G=0;local H=0;F=self:copy()H=F._len%128;if H<112 then H=112-H else H=240-H end;G=F._len*8;q=a.char(1<<7)..a.rep(a.char(0),H-1)..a.char(G>>120&0xFF)..a.char(G>>112&0xFF)..a.char(G>>104&0xFF)..a.char(G>>96&0xFF)..a.char(G>>88&0xFF)..a.char(G>>80&0xFF)..a.char(G>>72&0xFF)..a.char(G>>64&0xFF)..a.char(G>>56&0xFF)..a.char(G>>48&0xFF)..a.char(G>>40&0xFF)..a.char(G>>32&0xFF)..a.char(G>>24&0xFF)..a.char(G>>16&0xFF)..a.char(G>>8&0xFF)..a.char(G&0xFF)F:update(q)return F._H0:asbytestring()..F._H1:asbytestring()..F._H2:asbytestring()..F._H3:asbytestring()..F._H4:asbytestring()..F._H5:asbytestring()..F._H6:asbytestring()..F._H7:asbytestring()end;function c:hexdigest()local C;local I={}C=self:digest()for D=1,#C do I[D]=a.format("%02X",a.byte(C,D))end;return table.concat(I)end;return c
