←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft


Strange bug in SHA256 script

toxicwolf's Photo toxicwolf 01 Jul 2012

Hi all, I found a SHA256 Lua implementation a little while ago, but when using it, it causes the 'bit' CC api to throw an error complaining that it expected a number and received a nil value.

This is the source code. I've managed to track the error down to line 135, where it says:
H[1] = band(H[1], a)
Yet, it is giving the function two numbers, since I checked this by adding type() checks for both 'H[1]' and 'a', and then again actually at the beginning of the band function in the bit api.
So I have no idea what the problem is, hopefully someone can help...

Thanks in advance,
Toxic Wolf

RoD's Photo RoD 21 Jun 2013

The dropbox external link isn't working.

KFAFSP's Photo KFAFSP 21 Jun 2013

I just finished my own SHA-1 and SHA-256 implementation and had to do alot of fixing concerning the bit api. It just doesnt want to work correctly with DWORD values. Anyway, there actually was not any problem with ANDs. Are you sure this line is even correct, shouldn't it rather be something like :

H[1] = (H[1] + A) % (2 ^ 32)

Because it looks like the round-end hash adding operation to me, that you process after each digested chunk. Anyways, I had problems with bit.brshift (adding 1's on the left side...) and A + B exceeding 32bit value (fix is given in example, just do modulo). I cannot help you with the current problem, as I don't see a solution either. However, maybe you should take a closer look at the Library itself and maybe find another workaround.