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

Need help with SHA hashing.

Started by Creator, 02 February 2015 - 06:45 PM
Creator #1
Posted 02 February 2015 - 07:45 PM
Hi guys,

I was searching the internet for a hashing program and found this program:

Spoiler

-------------------------------------------------
---	  *** SHA-1 algorithm for Lua ***	  ---
-------------------------------------------------
--- Author:  Martin Huesser				   ---
--- Date:	2008-06-16					   ---
--- License: You may use this code in your	---
---		  projects as long as this header  ---
---		  stays intact.					---
-------------------------------------------------
local strlen  = string.len
local strchar = string.char
local strbyte = string.byte
local strsub  = string.sub
local floor   = math.floor
local bnot	= bit.bnot
local band	= bit.band
local bor	 = bit.bor
local bxor	= bit.bxor
local shl	 = bit.lshift
local shr	 = bit.rshift
local h0, h1, h2, h3, h4
-------------------------------------------------
local function LeftRotate(val, nr)
return shl(val, nr) + shr(val, 32 - nr)
end
-------------------------------------------------
local function ToHex(num)
local i, d
local str = ""
for i = 1, 8 do
  d = band(num, 15)
  if (d < 10) then
   str = strchar(d + 48) .. str
  else
   str = strchar(d + 87) .. str
  end
  num = floor(num / 16)
end
return str
end
-------------------------------------------------
local function PreProcess(str)
local bitlen, i
local str2 = ""
bitlen = strlen(str) * 8
str = str .. strchar(128)
i = 56 - band(strlen(str), 63)
if (i < 0) then
  i = i + 64
end
for i = 1, i do
  str = str .. strchar(0)
end
for i = 1, 8 do
  str2 = strchar(band(bitlen, 255)) .. str2
  bitlen = floor(bitlen / 256)
end
return str .. str2
end
-------------------------------------------------
local function MainLoop(str)
local a, b, c, d, e, f, k, t
local i, j
local w = {}
while (str ~= "") do
  for i = 0, 15 do
   w[i] = 0
   for j = 1, 4 do
	w[i] = w[i] * 256 + strbyte(str, i * 4 + j)
   end
  end
  for i = 16, 79 do
   w[i] = LeftRotate(bxor(bxor(w[i - 3], w[i - 8]), bxor(w[i - 14], w[i - 16])), 1)
  end
  a = h0
  b = h1
  c = h2
  d = h3
  e = h4
  for i = 0, 79 do
   if (i < 20) then
	f = bor(band(b, c), band(bnot(B)/>/>/>/>/>/>, d))
	k = 1518500249
   elseif (i < 40) then
	f = bxor(bxor(b, c), d)
	k = 1859775393
   elseif (i < 60) then
	f = bor(bor(band(b, c), band(b, d)), band(c, d))
	k = 2400959708
   else
	f = bxor(bxor(b, c), d)
	k = 3395469782
   end
   t = LeftRotate(a, 5) + f + e + k + w[i]
   e = d
   d = c
   c = LeftRotate(b, 30)
   b = a
   a = t
  end
  h0 = band(h0 + a, 4294967295)
  h1 = band(h1 + b, 4294967295)
  h2 = band(h2 + c, 4294967295)
  h3 = band(h3 + d, 4294967295)
  h4 = band(h4 + e, 4294967295)
  str = strsub(str, 65)
end
end
-------------------------------------------------
function Sha1(str)
str = PreProcess(str)
h0  = 1732584193
h1  = 4023233417
h2  = 2562383102
h3  = 0271733878
h4  = 3285377520
MainLoop(str)
return  ToHex(h0) ..
  ToHex(h1) ..
  ToHex(h2) ..
  ToHex(h3) ..
  ToHex(h4)
end
-------------------------------------------------
-------------------------------------------------
-------------------------------------------------

So when I start lua in the shell and then type os.loadAPI("SHA") and then type sha.sha1("hi") it says that there was an error on line 27: attempt to call nil. I have no idea why it is happening. Any help would be appreciated.

Thanks
InDieTasten #2
Posted 02 February 2015 - 08:10 PM
We cannot know what line 27 is, because the spacing was removed from your code-section. please insert the code by hand between the code tags to avoid manipulation by the forums

Or just upload to pastebin, which gets rid of all problems, because the forums code snippets s*ck
Creator #3
Posted 02 February 2015 - 08:17 PM
We cannot know what line 27 is, because the spacing was removed from your code-section. please insert the code by hand between the code tags to avoid manipulation by the forums

Or just upload to pastebin, which gets rid of all problems, because the forums code snippets s*ck

Sorry, line 27 is the line in the middle of this piece of code:


local function LeftRotate(val, nr)
  return shl(val, nr) + shr(val, 32 - nr)
end
Edited on 02 February 2015 - 07:17 PM
InDieTasten #4
Posted 02 February 2015 - 08:25 PM
We cannot know what line 27 is, because the spacing was removed from your code-section. please insert the code by hand between the code tags to avoid manipulation by the forums

Or just upload to pastebin, which gets rid of all problems, because the forums code snippets s*ck

Sorry, line 27 is the line in the middle of this piece of code:


local function LeftRotate(val, nr)
  return shl(val, nr) + shr(val, 32 - nr)
end
You/the API are/is calling shl and shr, which are set to bit.lshift and bit.rshift. it seems these functions are not available in your environment

Here you can see they are not available in the names given in the api, but you can change them to blshift and brshift to fix that part. I wouldn't be surprised, if there are lots of other problems approaching you afterwards
Creator #5
Posted 02 February 2015 - 08:29 PM
We cannot know what line 27 is, because the spacing was removed from your code-section. please insert the code by hand between the code tags to avoid manipulation by the forums

Or just upload to pastebin, which gets rid of all problems, because the forums code snippets s*ck

Sorry, line 27 is the line in the middle of this piece of code:


local function LeftRotate(val, nr)
  return shl(val, nr) + shr(val, 32 - nr)
end
You/the API are/is calling shl and shr, which are set to bit.lshift and bit.rshift. it seems these functions are not available in your environment

Here you can see they are not available in the names given in the api, but you can change them to blshift and brshift to fix that part. I wouldn't be surprised, if there are lots of other problems approaching you afterwards

You are so right. It now says java exception from thrown.
Bomb Bloke #6
Posted 02 February 2015 - 09:28 PM
Intersperse print statements throughout your script. By checking which ones fire before the "exception" error, placing more in that area of the script and so on, you should be able to narrow it down to a particular line.
DannySMc #7
Posted 03 February 2015 - 11:09 AM
I don't get why you don't use the SHA256 hashing algorithm already implemented in lua and works in computercraft because I use it? It's a lot easier!:D/> if you want it just tell me, will post it underneath
Anavrins #8
Posted 03 February 2015 - 12:50 PM
GravityScore's SHA256 here
Should work without modification, and is way more secure than SHA1 or MD5.
Creator #9
Posted 03 February 2015 - 03:18 PM
I don't get why you don't use the SHA256 hashing algorithm already implemented in lua and works in computercraft because I use it? It's a lot easier! :D/> if you want it just tell me, will post it underneath

Thanks man.