This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
The Crazy Phoenix's profile picture

Looking for a hashing API in CC

Started by The Crazy Phoenix, 06 July 2016 - 08:13 AM
The Crazy Phoenix #1
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.
The Crazy Phoenix #2
Posted 06 July 2016 - 02:28 PM
Nevermind, I ended up having to implement my own SHA-1 function for MGF1 anyway.

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

Edit: Formatting seems to mess up.
Edited on 06 July 2016 - 12:32 PM
KingofGamesYami #3
Posted 06 July 2016 - 03:03 PM
For future reference, sha256 has already been ported to CC.
Anavrins #4
Posted 06 July 2016 - 03:42 PM
I have quite a few on my profile page http://www.computercraft.info/forums2/index.php?/user/12870-anavrins/
I would suggest sha256 though.
The Crazy Phoenix #5
Posted 06 July 2016 - 03:56 PM
I would suggest sha256 though.

I'm only using the hashing algorithm for MGF1 and certificate validation anyway.