-- Lua-hashings @89879fe79b6f3dc495c607494126ec9c3912b8e9 | /hashings/sha256.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=32;c.block_size=64;local e={b(0x428A2F98),b(0x71374491),b(0xB5C0FBCF),b(0xE9B5DBA5),b(0x3956C25B),b(0x59F111F1),b(0x923F82A4),b(0xAB1C5ED5),b(0xD807AA98),b(0x12835B01),b(0x243185BE),b(0x550C7DC3),b(0x72BE5D74),b(0x80DEB1FE),b(0x9BDC06A7),b(0xC19BF174),b(0xE49B69C1),b(0xEFBE4786),b(0x0FC19DC6),b(0x240CA1CC),b(0x2DE92C6F),b(0x4A7484AA),b(0x5CB0A9DC),b(0x76F988DA),b(0x983E5152),b(0xA831C66D),b(0xB00327C8),b(0xBF597FC7),b(0xC6E00BF3),b(0xD5A79147),b(0x06CA6351),b(0x14292967),b(0x27B70A85),b(0x2E1B2138),b(0x4D2C6DFC),b(0x53380D13),b(0x650A7354),b(0x766A0ABB),b(0x81C2C92E),b(0x92722C85),b(0xA2BFE8A1),b(0xA81A664B),b(0xC24B8B70),b(0xC76C51A3),b(0xD192E819),b(0xD6990624),b(0xF40E3585),b(0x106AA070),b(0x19A4C116),b(0x1E376C08),b(0x2748774C),b(0x34B0BCB5),b(0x391C0CB3),b(0x4ED8AA4A),b(0x5B9CCA4F),b(0x682E6FF3),b(0x748F82EE),b(0x78A5636F),b(0x84C87814),b(0x8CC70208),b(0x90BEFFFA),b(0xA4506CEB),b(0xBEF9A3F7),b(0xC67178F2)}local function f(g,h)return g>>h|(g<<32-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,2)~f(g,13)~f(g,22)end;local function n(g)return f(g,6)~f(g,11)~f(g,25)end;local function o(g)return f(g,7)~f(g,18)~(g>>3)end;local function p(g)return f(g,17)~f(g,19)~(g>>10)end;function c:new(q)if self~=c then return nil,"First argument must be self"end;local r=setmetatable({},d)r._H0=b(0x6A09E667)r._H1=b(0xBB67AE85)r._H2=b(0x3C6EF372)r._H3=b(0xA54FF53A)r._H4=b(0x510E527F)r._H5=b(0x9B05688C)r._H6=b(0x1F83D9AB)r._H7=b(0x5BE0CD19)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>=64 do s={}for D=1,64,4 do local E=#s+1;s[E]=b(a.byte(self._data,D))<<24;s[E]=s[E]|(b(a.byte(self._data,D+1))<<16)s[E]=s[E]|(b(a.byte(self._data,D+2))<<8)s[E]=s[E]|b(a.byte(self._data,D+3))end;self._data=self._data:sub(65,#self._data)for D=17,64 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,64 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%64;if H<56 then H=56-H else H=120-H end;G=F._len*8;q=a.char(1<<7)..a.rep(a.char(0),H-1)..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
