-- Lua-hashings @89879fe79b6f3dc495c607494126ec9c3912b8e9 | /hashings/blake2b.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=require("nums.uintn").u8;local d={}local e={__metatable={},__index=d}d.digest_size=64;d.block_size=128;local f={b("0x6A09E667F3BCC908"),b("0xBB67AE8584CAA73B"),b("0x3C6EF372FE94F82B"),b("0xA54FF53A5F1D36F1"),b("0x510E527FADE682D1"),b("0x9B05688C2B3E6C1F"),b("0x1F83D9ABFB41BD6B"),b("0x5BE0CD19137E2179")}local g={{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},{15,11,5,9,10,16,14,7,2,13,1,3,12,8,6,4},{12,9,13,1,6,3,16,14,11,15,4,7,8,2,10,5},{8,10,4,2,14,13,12,15,3,7,6,11,5,1,16,9},{10,1,6,8,3,5,11,16,15,2,12,13,7,9,4,14},{3,13,7,11,1,12,9,4,5,14,8,6,16,15,2,10},{13,6,2,16,15,14,5,11,1,8,7,4,10,3,9,12},{14,12,8,15,13,2,4,10,6,1,16,5,9,7,3,11},{7,16,15,10,12,4,1,9,13,3,14,8,2,5,11,6},{11,3,9,5,8,7,2,6,16,12,10,15,4,13,14,1},{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},{15,11,5,9,10,16,14,7,2,13,1,3,12,8,6,4}}local function h(i,j)return i>>j|(i<<64-j)end;local function k(l,m)l._t0=l._t0+m;if l._t0<m then l._t1=l._t1+1 end end;local function n(o,p,q,r,s,t,u)r=r+s+q[g[o+1][2*p+1]]u=h(u~r,32)t=t+u;s=h(s~t,24)r=r+s+q[g[o+1][2*p+2]]u=h(u~r,16)t=t+u;s=h(s~t,63)return r,s,t,u end;local function v(o,q,w)w[1],w[5],w[9],w[13]=n(o,0,q,w[1],w[5],w[9],w[13])w[2],w[6],w[10],w[14]=n(o,1,q,w[2],w[6],w[10],w[14])w[3],w[7],w[11],w[15]=n(o,2,q,w[3],w[7],w[11],w[15])w[4],w[8],w[12],w[16]=n(o,3,q,w[4],w[8],w[12],w[16])w[1],w[6],w[11],w[16]=n(o,4,q,w[1],w[6],w[11],w[16])w[2],w[7],w[12],w[13]=n(o,5,q,w[2],w[7],w[12],w[13])w[3],w[8],w[9],w[14]=n(o,6,q,w[3],w[8],w[9],w[14])w[4],w[5],w[10],w[15]=n(o,7,q,w[4],w[5],w[10],w[15])end;local function x(l)local w={}local q={}if#l._data<128 then return end;for y=1,128,8 do q[#q+1]=b(a.byte(l._data,y+7))<<56|(b(a.byte(l._data,y+6))<<48)|(b(a.byte(l._data,y+5))<<40)|(b(a.byte(l._data,y+4))<<32)|(b(a.byte(l._data,y+3))<<24)|(b(a.byte(l._data,y+2))<<16)|(b(a.byte(l._data,y+1))<<8)|b(a.byte(l._data,y))end;l._data=l._data:sub(129,#l._data)for p=1,8 do w[p]=l._h[p]:copy()end;w[9]=f[1]:copy()w[10]=f[2]:copy()w[11]=f[3]:copy()w[12]=f[4]:copy()w[13]=l._t0~f[5]w[14]=l._t1~f[6]if l._last then w[15]=b("0xFFFFFFFFFFFFFFFF")~f[7]else w[15]=f[7]:copy()end;w[16]=f[8]:copy()v(0,q,w)v(1,q,w)v(2,q,w)v(3,q,w)v(4,q,w)v(5,q,w)v(6,q,w)v(7,q,w)v(8,q,w)v(9,q,w)v(10,q,w)v(11,q,w)for p=1,8 do l._h[p]=l._h[p]~w[p]~w[p+8]end end;function d:new(z)if self~=d then return nil,"First argument must be self"end;local A=setmetatable({},e)A._h={}for p=1,8 do A._h[p]=f[p]:copy()end;A._h[1]=A._h[1]~16842816;A._last=false;A._t0=b(0)A._t1=b(0)A._data=""if z~=nil then A:update(z)end;return A end;setmetatable(d,{__call=d.new})function d:copy()local A=d()for p=1,8 do A._h[p]=self._h[p]:copy()end;A._last=self._last;A._t0=self._t0:copy()A._t1=self._t1:copy()A._data=self._data;return A end;function d:update(z)if z==nil then z=""end;z=tostring(z)self._data=self._data..z;while#self._data>128 do k(self,128)x(self)end end;function d:digest()local B;local C={}local y;B=self:copy()k(B,#B._data)B._last=true;B._data=B._data..a.rep(a.char(0),128-#B._data)x(B)for p=1,#B._h do C[p]=B._h[p]:swape():asbytestring()end;return table.concat(C)end;function d:hexdigest()local D;local C={}D=self:digest()for p=1,#D do C[p]=a.format("%02X",a.byte(D,p))end;return table.concat(C)end;return d
