View difference between Paste ID: 4e6hjKBF and gsFrNjbt
SHOW: | | - or go back to the newest paste.
1-
1+
2
--  Adaptation of the Secure Hashing Algorithm (SHA-244/256)
3
--  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
4
--  
5
--  Using an adapted version of the bit library
6
--  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
7
--  
8
--  API-d by randomdude999
9-
9+
-- 
10-
local MOD = 2^32
10+
local MOD=2^32
11-
local MODM = MOD-1
11+
local MODM=MOD-1
12-
12+
13
	local mt={}
14-
	local mt = {}
14+
	local t=setmetatable({}, mt)
15-
	local t = setmetatable({}, mt)
15+
16
		local v=f(k)
17-
		local v = f(k)
17+
		t[k]=v
18-
		t[k] = v
18+
19
	end
20
	return t
21
end
22
local function make_bitop_uncached(t,m)
23-
23+
	local function bitop(a,b)
24-
local function make_bitop_uncached(t, m)
24+
		local res,p=0,1
25-
	local function bitop(a, b)
25+
		while a~=0 and b~=0 do
26-
		local res,p = 0,1
26+
			local am,bm=a%m,b%m
27-
		while a ~= 0 and b ~= 0 do
27+
			res=res+t[am][bm]*p
28-
			local am, bm = a % m, b % m
28+
			a=(a-am)/m
29-
			res = res + t[am][bm] * p
29+
			b=(b-bm)/m
30-
			a = (a - am) / m
30+
			p=p*m
31-
			b = (b - bm) / m
31+
32-
			p = p*m
32+
		res=res+(a+b)*p
33
		return res
34-
		res = res + (a + b) * p
34+
35
	return bitop
36
end
37
local function make_bitop(t)
38
	local op1=make_bitop_uncached(t,2^1)
39-
39+
	local op2=memoize(function(a) return memoize(function(b) return op1(a,b) end) end)
40
	return make_bitop_uncached(op2,2^(t.n or 1))
41-
	local op1 = make_bitop_uncached(t,2^1)
41+
42-
	local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
42+
local bxor1 = make_bitop({[0]={[0]=0,[1]=1},[1]={[0]=1,[1]=0},n=4})
43-
	return make_bitop_uncached(op2, 2 ^ (t.n or 1))
43+
local function bxor(a,b,c,...)
44
	local z=nil
45-
45+
46-
local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
46+
		a=a%MOD
47-
47+
		b=b%MOD
48-
local function bxor(a, b, c, ...)
48+
		z=bxor1(a,b)
49-
	local z = nil
49+
		if c then z=bxor(z,c,...) end
50
		return z
51-
		a = a % MOD
51+
	elseif a then return a%MOD
52-
		b = b % MOD
52+
53-
		z = bxor1(a, b)
53+
54-
		if c then z = bxor(z, c, ...) end
54+
local function band(a,b,c,...)
55
	local z
56-
	elseif a then return a % MOD
56+
57
		a=a%MOD
58
		b=b%MOD
59-
59+
		z=((a+b)-bxor1(a,b))/2
60-
local function band(a, b, c, ...)
60+
		if c then z=bit32_band(z,c,...) end
61
		return z
62
	elseif a then return a%MOD
63-
		a = a % MOD
63+
64-
		b = b % MOD
64+
65-
		z = ((a + b) - bxor1(a,b)) / 2
65+
local function bnot(x) return (-1-x)%MOD end
66-
		if c then z = bit32_band(z, c, ...) end
66+
local function rshift1(a,disp)
67
	if disp<0 then return lshift(a,-disp) end
68-
	elseif a then return a % MOD
68+
	return math.floor(a%MOD/2^disp)
69
end
70
local function rshift(x,disp)
71-
71+
	if disp>31 or disp<-31 then return 0 end
72-
local function bnot(x) return (-1 - x) % MOD end
72+
	return rshift1(x%MOD,disp)
73-
73+
74-
local function rshift1(a, disp)
74+
local function lshift(a,disp)
75-
	if disp < 0 then return lshift(a,-disp) end
75+
	if disp<0 then return rshift(a,-disp) end 
76-
	return math.floor(a % 2 ^ 32 / 2 ^ disp)
76+
	return (a*2^disp)%MOD
77
end
78-
78+
local function rrotate(x,disp)
79-
local function rshift(x, disp)
79+
    x=x%MOD
80-
	if disp > 31 or disp < -31 then return 0 end
80+
    disp=disp%32
81-
	return rshift1(x % MOD, disp)
81+
    local low=band(x,2^disp-1)
82
    return rshift(x,disp)+lshift(low,32-disp)
83-
83+
84-
local function lshift(a, disp)
84+
85-
	if disp < 0 then return rshift(a,-disp) end 
85+
	0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
86-
	return (a * 2 ^ disp) % 2 ^ 32
86+
	0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
87
	0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
88-
88+
	0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
89-
local function rrotate(x, disp)
89+
	0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
90-
    x = x % MOD
90+
	0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
91-
    disp = disp % 32
91+
	0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
92-
    local low = band(x, 2 ^ disp - 1)
92+
	0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
93-
    return rshift(x, disp) + lshift(low, 32 - disp)
93+
	0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
94
	0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
95-
95+
	0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
96
	0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
97-
	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
97+
	0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
98-
	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
98+
	0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
99-
	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
99+
	0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
100-
	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
100+
	0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2,
101-
	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
101+
102-
	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
102+
103-
	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
103+
	return (string.gsub(s,".",function(c) return string.format("%02x",string.byte(c)) end))
104-
	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
104+
105-
	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
105+
local function num2s(l,n)
106-
	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
106+
	local s=""
107-
	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
107+
	for i=1,n do
108-
	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
108+
		local rem=l%256
109-
	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
109+
		s=string.char(rem)..s
110-
	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
110+
		l=(l-rem)/256
111-
	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
111+
112-
	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
112+
113
end
114-
114+
local function s232num(s,i)
115
	local n=0
116-
	return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
116+
	for i=i,i+3 do n=n*256+string.byte(s,i) end
117
	return n
118-
118+
119-
local function num2s(l, n)
119+
local function preproc(a, b)
120-
	local s = ""
120+
	local c=64-((b+9)%64)
121-
	for i = 1, n do
121+
	b=num2s(8*b,8)
122-
		local rem = l % 256
122+
	a=a.."\128"..string.rep("\0",c)..b
123-
		s = string.char(rem) .. s
123+
	assert(#a%64==0)
124-
		l = (l - rem) / 256
124+
	return a
125
end
126
local function digestblock(msg,i,H)
127
	local w={}
128-
128+
	for j=1,16 do w[j]=s232num(msg,i+(j-1)*4) end
129-
local function s232num(s, i)
129+
	for j=17,64 do
130-
	local n = 0
130+
		local v=w[j-15]
131-
	for i = i, i + 3 do n = n*256 + string.byte(s, i) end
131+
		local s0=bxor(rrotate(v,7),rrotate(v,18),rshift(v,3))
132
		v=w[j-2]
133
		w[j]=w[j-16]+s0+w[j-7]+bxor(rrotate(v,17),rrotate(v,19),rshift(v,10))
134-
134+
135-
local function preproc(msg, len)
135+
	local a,b,c,d,e,f,g,h=H[1],H[2],H[3],H[4],H[5],H[6],H[7],H[8]
136-
	local extra = 64 - ((len + 9) % 64)
136+
	for i=1,64 do
137-
	len = num2s(8 * len, 8)
137+
		local s0=bxor(rrotate(a,2),rrotate(a,13),rrotate(a,22))
138-
	msg = msg .. "\128" .. string.rep("\0", extra) .. len
138+
		local maj=bxor(band(a,b),band(a,c), band(b,c))
139-
	assert(#msg % 64 == 0)
139+
		local t2=s0+maj
140-
	return msg
140+
		local s1=bxor(rrotate(e,6),rrotate(e,11),rrotate(e,25))
141
		local ch=bxor(band(e,f),band(bnot(e),g))
142-
142+
		local t1=h+s1+ch+k[i]+w[i]
143-
local function initH256(H)
143+
		h,g,f,e,d,c,b,a=g,f,e,d+t1,c,b,a,t1+t2
144-
	H[1] = 0x6a09e667
144+
145-
	H[2] = 0xbb67ae85
145+
	H[1]=band(H[1]+a)
146-
	H[3] = 0x3c6ef372
146+
	H[2]=band(H[2]+b)
147-
	H[4] = 0xa54ff53a
147+
	H[3]=band(H[3]+c)
148-
	H[5] = 0x510e527f
148+
	H[4]=band(H[4]+d)
149-
	H[6] = 0x9b05688c
149+
	H[5]=band(H[5]+e)
150-
	H[7] = 0x1f83d9ab
150+
	H[6]=band(H[6]+f)
151-
	H[8] = 0x5be0cd19
151+
	H[7]=band(H[7]+g)
152-
	return H
152+
	H[8]=band(H[8]+h)
153
end
154-
154+
function sha256(a)
155-
local function digestblock(msg, i, H)
155+
	a = preproc(a, #a)
156-
	local w = {}
156+
	local H = {0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}
157-
	for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
157+
	for i = 1, #a, 64 do digestblock(a, i, H) end
158-
	for j = 17, 64 do
158+
	return str2hexa(num2s(H[1],4)..num2s(H[2],4)..num2s(H[3],4)..num2s(H[4],4)..num2s(H[5],4)..num2s(H[6],4)..num2s(H[7],4)..num2s(H[8],4))
159-
		local v = w[j - 15]
159+