Created
November 29, 2017 08:54
-
-
Save operator-DD3/f98b8c4170ab2f7d7e7d8097c6ab2fad to your computer and use it in GitHub Desktop.
New Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function log2 (x) return math.log(x) / math.log(2) end | |
function entropy (X) | |
local N, count, sum, i = X:len(), {}, 0 | |
for char = 1, N do | |
i = X:sub(char, char) | |
if count[i] then | |
count[i] = count[i] + 1 | |
else | |
count[i] = 1 | |
end | |
end | |
for n_i, count_i in pairs(count) do | |
sum = sum + count_i / N * log2(count_i / N) | |
end | |
return -sum | |
end | |
local hexToBin_lut={ ["0"]="0000", ["1"]="0001", ["2"]="0010", ["3"]="0011", ["4"]="0100", ["5"]="0101", ["6"]="0110", ["7"]="0111", ["8"]="1000", ["9"]="1001", ["a"]="1010", ["b"]="1011", ["c"]="1100", ["d"]="1101", ["e"]="1110", ["f"]="1111", } | |
local binToHex_lut={ ["0000"]="0", ["0001"]="1", ["0010"]="2", ["0011"]="3", ["0100"]="4", ["0101"]="5", ["0110"]="6", ["0111"]="7", ["1000"]="8", ["1001"]="9", ["1010"]="a", ["1011"]="b", ["1100"]="c", ["1101"]="d", ["1110"]="e", ["1111"]="f", } | |
local function toBinary(num) | |
return string.format("%x",num):gsub(".",function(v) return hexToBin_lut[v] end):match("^[0]*(%d*)$") | |
end | |
function string.hexbin(str) | |
local bin = "" | |
for i=1,#str do | |
bin = bin .. hexToBin_lut[string.sub(str,i,i)] | |
end | |
return bin | |
end | |
function string.binhex(str) | |
local res = "" | |
for i=1,#str,4 do | |
res = res .. binToHex_lut[string.sub(str,i,i+3)] | |
end | |
return res | |
end | |
function string.to_hex(str) | |
return (str:gsub('.', function(c) | |
return string.lower(string.format('%02x', string.byte(c))) | |
end)) | |
end | |
function string.from_hex(str) | |
return (str:gsub('..', function(cc) | |
return string.char(tonumber(cc,16)) | |
end)) | |
end | |
function rand(n) | |
t = {} | |
for i = 1, n do | |
t[i] = string.char(math.random(0,255)) | |
end | |
--return table.concat(t) | |
return block(tostring(table.concat(t)), n*8) | |
end | |
function xor(a,b) | |
local t = {} | |
for i = 1, #a do | |
t[i] = string.char(bit.bxor(string.byte(a,i,i),string.byte(b,i,i))) | |
end | |
return table.concat(t) | |
end | |
function block(message, blocksize, stsz, rounds) | |
blocksize = blocksize or 256 | |
blocksize2=blocksize*4 | |
local rounds = rounds or 4 | |
local stsz = stsz or 128--bytes | |
local stm1 = stsz - 1 | |
local b, c, d, e = sb or 3, sc or 5, sd or 8, se or 13 | |
local state = {} | |
local rsl = "0" | |
local hash = {} | |
for j = 1, #message do | |
for k=1,rounds do | |
for i = 1, stsz do | |
e = ((state[bit.bxor(d,stm1)] or 0) * string.byte(string.sub(message,j,j))) | |
state[bit.bxor(d,stm1)+(e%2)] = bit.bxor(b,c) | |
state[i]= - bit.bnot(bit.bxor(b,c)) | |
b = (c + d)%256 | |
c = (d + e)%256 | |
d = e + (state[i] or 0) | |
rsl = (rsl + d) % 256 | |
end | |
end | |
end | |
for j = 1, blocksize2/4 do | |
for k=1,rounds do | |
for i = 1, stsz do | |
e = (state[bit.bxor(d,stm1)] or 0) + (state[i] or 0) | |
state[bit.bxor(d,stm1)+(e%stsz)+(e%2)] = bit.bnot(bit.bxor(b,c)) | |
state[i]= - bit.bnot(bit.bxor(b,c)) | |
b = (c + d)%256 | |
c = (d + e)%256 | |
d = e + (state[i] or 0) | |
rsl = (rsl + d) % 256 | |
--io.write("\ns[",i,"]=",state[i] or '00') | |
end | |
end | |
hash[j] = string.format("%02x", rsl)--:from_hex() | |
end | |
--return table.concat(hash):to_binary()--:to_hex() | |
local binary = table.concat(hash):hexbin() | |
local res = "" | |
for i=1,#binary,2 do --VON-NEUMAN EXTRACTOR | |
if string.sub(binary,i,i) ~= string.sub(binary,i+1,i+1) then | |
res = res .. string.sub(binary,i,i) | |
end | |
end | |
while #res%4~=0 do | |
res=res.."0" | |
end | |
res=string.binhex(res) | |
return string.sub(res,1,blocksize/4) | |
--return xor(string.sub(res,1,blocksize/4):from_hex(),table.concat(hash):from_hex()):to_hex() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment