#1
Posted 05 January 2013 - 09:38 AM
Quite recently, I've been finding myself needing some very secure hashing functions to encrypt passwords and other data. In light of recent conversations on these forums, it seemed like SHA-1 just wasn't going to cut it.
I started searching and came across SHA-2 - a set of functions that are far more secure than SHA-1. They include SHA-244, SHA-256, SHA-384, and SHA-512. I found a pure Lua implementation of both SHA-244 and SHA-256 here, in the Lua documentation (adapted from the Pseudo code found on Wikipedia here). This seemed like it would work perfectly.
I fired it up with a test example, and found that the standard ComputerCraft bit API couldn't deal with the large numbers that were being produced by this, and also didn't have a right rotate function - so I found a custom implementation of the Lua bit32, and included it.
I vastly adapted both sources, and compiled them into easily copy and paste-able pieces of code (no need to install an API). Albeit, there may be far more speed and size efficient ways (it's around 200 lines) of implementing this, but they work.
In regards to copyright: both of these sources allow the free use of the code in them - they do not require you to include credits or licenses (to be honest, they aren't very clear about who exactly wrote them...).
SHA-256 can be downloaded from Pastebin using the ID: gsFrNjbt.
#2
Posted 05 January 2013 - 12:10 PM
On a separate note how are you testing the speed?
#3
Posted 05 January 2013 - 12:34 PM
#4
Posted 05 January 2013 - 01:04 PM
Also, doing all in one function might seem cleaner, but since each internal function is re-made (not sure exactly of the terminology) each time you call sha244, it is actually worst efficiency-wise.
I have a bench marking module (github) that generates nice-looking results and comparisons between given functions; can't really spend time doing it now with sha.
#5
Posted 05 January 2013 - 09:17 PM
CoolisTheName007, on 05 January 2013 - 01:04 PM, said:
Also, doing all in one function might seem cleaner, but since each internal function is re-made (not sure exactly of the terminology) each time you call sha244, it is actually worst efficiency-wise.
I have a bench marking module (github) that generates nice-looking results and comparisons between given functions; can't really spend time doing it now with sha.
Yes the SHA1 I was using wasn't yours that used the CC bit API. Maybe that will be faster - I'm not too concerned about speed with this.
Like I said in the OP, I would have used the local CC bit API, but I couldn't because it gave me the error "number is too large (maximum allowed: 2^32-1)", and it also did not have an implementation of right rotate (rrotate). Would it be better if I stored each function in a local variable? (like local band = function(int1, int2, int3, ...)?)
#6
Posted 05 January 2013 - 10:37 PM
GravityScore, on 05 January 2013 - 09:17 PM, said:
OFF TOPIC:
GravityScore, on 05 January 2013 - 09:17 PM, said:
#8
Posted 06 January 2013 - 05:13 AM
CoolisTheName007, on 06 January 2013 - 04:22 AM, said:
Just note this isn't supposed to be an API. I hate programs that make me install APIs. This is intended to be copied and pasted straight into code. I guess I can move the bit functions outside of the function though. I'll do it now! Thanks for the suggestion.
#9
Posted 06 January 2013 - 07:21 AM
GravityScore, on 06 January 2013 - 05:13 AM, said:
To make sure the only function exposed is sha22 to an user that copied and pasted your code into a program, surround the whole code with a do ... end block. That will keep the local functions to that block, and only sha244 will be put into the program's environment:
do
local function band(..)
..
end
function sha244(..)
..
end
end
I use this more and more to keep blocks of code contained to themselves in the same file.
#10
Posted 04 February 2013 - 11:15 AM
#11
Posted 05 February 2013 - 03:17 AM
local stringToHash = "thisisapassword" hash = sha256(stringToHash)
#12
Posted 05 February 2013 - 09:46 AM
#13
Posted 05 February 2013 - 02:53 PM
DiamondOwner, on 05 February 2013 - 09:46 AM, said:
DiamondOwner, on 05 February 2013 - 09:46 AM, said:
DiamondOwner, on 05 February 2013 - 09:46 AM, said:
#14
Posted 10 February 2013 - 04:41 PM
GravityScore, on 05 January 2013 - 09:17 PM, said:
Like I said in the OP, I would have used the local CC bit API, but I couldn't because it gave me the error "number is too large (maximum allowed: 2^32-1)", and it also did not have an implementation of right rotate (rrotate). Would it be better if I stored each function in a local variable? (like local band = function(int1, int2, int3, ...)?)
I just wrote my own implementation of SHA-2, and ran into the same problem you did. Instead of doing everything with the standard operators as you did, I just used modulus on pretty much every variable in the code by (2^32-1) that was used in addition operations.
Also, as for the second question, you can simply put all of the functions used within the function inside the function body itself. Lua has no problems with functions inside of functions.
#16
#17
Posted 11 February 2013 - 05:50 AM
#18
Posted 11 February 2013 - 07:26 AM
#19
Posted 22 March 2013 - 08:45 PM
Can I use SHA256 for CUNIX root and other user password encryption? Thanks, Lin
#20
Posted 22 March 2013 - 09:19 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users