crypto cleanup
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
-- SHA-256, HMAC and PBKDF2 functions in ComputerCraft
|
||||
-- By Anavrins
|
||||
|
||||
local mod32 = 2^32
|
||||
local bit = _G.bit
|
||||
local os = _G.os
|
||||
local mod32 = 2^32
|
||||
local band = bit32 and bit32.band or bit.band
|
||||
local bnot = bit32 and bit32.bnot or bit.bnot
|
||||
local bxor = bit32 and bit32.bxor or bit.bxor
|
||||
@@ -38,7 +40,7 @@ local function counter(incr)
|
||||
local t1, t2 = 0, 0
|
||||
if 0xFFFFFFFF - t1 < incr then
|
||||
t2 = t2 + 1
|
||||
t1 = incr - (0xFFFFFFFF - t1) - 1
|
||||
t1 = incr - (0xFFFFFFFF - t1) - 1
|
||||
else t1 = t1 + incr
|
||||
end
|
||||
return t2, t1
|
||||
@@ -66,7 +68,7 @@ end
|
||||
|
||||
local function digestblock(w, C)
|
||||
for j = 17, 64 do
|
||||
local v = w[j-15]
|
||||
-- local v = w[j-15]
|
||||
local s0 = bxor(bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18)), brshift(w[j-15], 3))
|
||||
local s1 = bxor(bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19)), brshift(w[j-2], 10))
|
||||
w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod32
|
||||
@@ -95,7 +97,7 @@ end
|
||||
local mt = {
|
||||
__tostring = function(a) return string.char(unpack(a)) end,
|
||||
__index = {
|
||||
toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
|
||||
toHex = function(self) return ("%02x"):rep(#self):format(unpack(self)) end,
|
||||
isEqual = function(self, t)
|
||||
if type(t) ~= "table" then return false end
|
||||
if #self ~= #t then return false end
|
||||
@@ -120,7 +122,7 @@ local function toBytes(t, n)
|
||||
end
|
||||
|
||||
local function digest(data)
|
||||
local data = data or ""
|
||||
data = data or ""
|
||||
data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
|
||||
|
||||
data = preprocess(data)
|
||||
@@ -130,8 +132,8 @@ local function digest(data)
|
||||
end
|
||||
|
||||
local function hmac(data, key)
|
||||
local data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
|
||||
local key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
|
||||
data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
|
||||
key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
|
||||
|
||||
local blocksize = 64
|
||||
|
||||
@@ -161,9 +163,9 @@ local function hmac(data, key)
|
||||
end
|
||||
|
||||
local function pbkdf2(pass, salt, iter, dklen)
|
||||
local salt = type(salt) == "table" and salt or {tostring(salt):byte(1,-1)}
|
||||
salt = type(salt) == "table" and salt or {tostring(salt):byte(1,-1)}
|
||||
local hashlen = 32
|
||||
local dklen = dklen or 32
|
||||
dklen = dklen or 32
|
||||
local block = 1
|
||||
local out = {}
|
||||
|
||||
@@ -190,8 +192,13 @@ local function pbkdf2(pass, salt, iter, dklen)
|
||||
return setmetatable(out, mt)
|
||||
end
|
||||
|
||||
local function compute(data)
|
||||
return digest(data):toHex()
|
||||
end
|
||||
|
||||
return {
|
||||
digest = digest,
|
||||
compute = compute,
|
||||
hmac = hmac,
|
||||
pbkdf2 = pbkdf2,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user