Posted 06 July 2016 - 10:13 AM
Does anyone know where I can find a hashing API that uses an algorithm with few collisions? I need it for a project.
local function hashSHA1(value)
testType(value, "table", "value")
local h0, h1, h2, h3, h4 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
local msglength = #value * 8
value:insert(0x80)
while #value % 64 ~= 56 do
value:insert(0)
end
for index = 0, 7 do
value:insert(bit32.band(bit32.rshift(msglength, index * 8), 255))
end
local chunkCount = #value / 64
for chunk = 0, chunkCount - 1 do
local dwords = {}
for index = 1, 16 do
dwords[index] = bit32.bor(bit32.lshift(value[chunk * 64 + index * 2 - 1], 8), value[chunk * 64 + index * 2])
end
for index = 17, 80 do
dwords[index] = bit32.lrotate(bit32.bxor(dwords[index - 3], dwords[index - 8], dwords[index - 14], dwords[index - 16]), 1)
end
local a, b, c, d, e = h0, h1, h2, h3, h4
for index = 0, 79 do
local f, k
if 0 <= index <= 19 then
f = bit32.bor(bit32.band(b, c), bit32.band(bit32.bnot(B)/>/>, d))
k = 0x5A827999
elseif 20 <= index <= 39 then
f = bit32.bxor(b, c, d)
k = 0x6ED9EBA1
elseif 40 <= index <= 59 then
f = bit32.bor(bit32.band(b, c), bit32.band(b, d), bit32.band(c, d))
k = 0x8F1BBCDC
else
f = bit32.bxor(b, c, d)
k = 0xCA62C1D6
end
local temp = bit32.lrotate(a, 5) + f + e + k + dwords[index + 1]
e = d
d = c
c = bit32.lrotate(b, 30)
b = a
a = temp
end
h0 = h0 + a
h1 = h1 + b
h2 = h2 + c
h3 = h3 + d
h4 = h4 + e
end
-- Write the 160-bit result as a byte table
local result = {}
for index = 0, 3 do
result[index + 1] = bit32.band(bit32.rshift(h0, 24 - index * 8), 255)
end
for index = 0, 3 do
result[index + 5] = bit32.band(bit32.rshift(h1, 24 - index * 8), 255)
end
for index = 0, 3 do
result[index + 9] = bit32.band(bit32.rshift(h2, 24 - index * 8), 255)
end
for index = 0, 3 do
result[index + 13] = bit32.band(bit32.rshift(h3, 24 - index * 8), 255)
end
for index = 0, 3 do
result[index + 17] = bit32.band(bit32.rshift(h4, 24 - index * 8), 255)
end
return result
end
I would suggest sha256 though.