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

SHA-256 in Pure Lua

Started by GravityScore, 05 January 2013 - 08:38 AM
GravityScore #1
Posted 05 January 2013 - 09:38 AM
Hey all,

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.
theoriginalbit #2
Posted 05 January 2013 - 12:10 PM
Nice job. I think I will be replacing my SHA-1 with one of these, never found any done quite like this.

On a separate note how are you testing the speed?
GravityScore #3
Posted 05 January 2013 - 12:34 PM
I just was printing them one after another, and the SHA-1 to a noticeably longer amount of time than the SHA-244 or 256. :P/>
CoolisTheName007 #4
Posted 05 January 2013 - 01:04 PM
Maybe you were using a sha1 that didn't used the bit API? I adapted one to use it, I doubt that it will be slower.

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.
GravityScore #5
Posted 05 January 2013 - 09:17 PM
Maybe you were using a sha1 that didn't used the bit API? I adapted one to use it, I doubt that it will be slower.

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, …)?)
theoriginalbit #6
Posted 05 January 2013 - 10:37 PM
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.
I would have suggested a way to test for curiosity but because of the way that os.time() works in CC there went the easiest way :P/> not having any system time in milliseconds sucks sometimes :P/>



OFF TOPIC:
"number is too large (maximum allowed: 2^32-1)"
Yeh I had/may still have some issues (haven't overly tested it) with that atm with the Extended String Library and implementing Java's hashCode function…
CoolisTheName007 #7
Posted 06 January 2013 - 04:22 AM
Would it be better if I stored each function in a local variable? (like local band = function(int1, int2, int3, …)?)

Yes, but outside the sha244 function, like

local function band(…

end

function sha244(…)
GravityScore #8
Posted 06 January 2013 - 05:13 AM
Would it be better if I stored each function in a local variable? (like local band = function(int1, int2, int3, …)?)

Yes, but outside the sha244 function, like

local function band(…

end

function sha244(…)

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.
CoolisTheName007 #9
Posted 06 January 2013 - 07:21 AM
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.

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.
DiamondOwner #10
Posted 04 February 2013 - 11:15 AM
How do you run the hash code for sha256?
GravityScore #11
Posted 05 February 2013 - 03:17 AM
You can run the hash function by copy and pasting the code for the SHA-256 function into you program, then do something like:

local stringToHash = "thisisapassword"
hash = sha256(stringToHash)
DiamondOwner #12
Posted 05 February 2013 - 09:46 AM
Thx GravityScore. Will global variables also work with the hash function? I'm planning to make my own small OS, may I use your hash code in my OS (I will give you credit.)? Are collisions with the hash "sha256" highly unlikely, unlikely, likely, or highly likely? This is to determine if encryption is required to keep people from reliably using rainbow tables to find out the password. What's the speed of the hash? This will help me determine the timer needed to hash a change to the username/password. I'm new to Lua, so can you convert a program into a string, then hash the program? This is to verify the integrity of core programs. Will this function (the hash) work in parallel.waitForAll? If you read to this part and have answered the above :D/> .
theoriginalbit #13
Posted 05 February 2013 - 02:53 PM
Thx GravityScore. Will global variables also work with the hash function? I'm planning to make my own small OS, may I use your hash code in my OS (I will give you credit.)? Are collisions with the hash "sha256" highly unlikely, unlikely, likely, or highly likely? This is to determine if encryption is required to keep people from reliably using rainbow tables to find out the password. What's the speed of the hash?
http://en.wikipedia....f_SHA_functions

I'm new to Lua, so can you convert a program into a string, then hash the program?
You just open the programs file and read the contents, thats a string. It the program is big enough it may crash the computer with a failure to yield (although I haven't tested if it will with this one, it does it with others). But be aware, you cannot do a hash of a program and then store it in itself, because then the hash will change, so the best method is to have an external program that stores all the and compares all the hashes, then store ITS own hash in a file somewhere for comparison.

Will this function (the hash) work in parallel.waitForAll? If you read to this part and have answered the above :D/> .
Not quite. With parallel functions you need to give it a function pointer, and since a function pointer means you can provide parameters you would need to setup a function that gets called from the parallel, which then calls the hashing.
KillaVanilla #14
Posted 10 February 2013 - 04:41 PM
-snip-

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.
theoriginalbit #15
Posted 10 February 2013 - 05:19 PM
I just moduloed (spelling?) pretty much every variable in the code by (2^32-1)
I think it would be this?

I just used modulo
Or
I just used modulus
KillaVanilla #16
Posted 10 February 2013 - 07:38 PM
I just moduloed (spelling?) pretty much every variable in the code by (2^32-1)
I think it would be this?

I just used modulo
Or
I just used modulus
Okay, thanks for the insight.
bjornir90 #17
Posted 11 February 2013 - 05:50 AM
To make it de-hash the msg I just have to run the function again ?
remiX #18
Posted 11 February 2013 - 07:26 AM
No, you can't de-hash a hashed string. To check if a typed string is equal to an already-hased string, hash the typed one and then check if they're equal
Left #19
Posted 22 March 2013 - 08:45 PM
Hello, Gravity.

Can I use SHA256 for CUNIX root and other user password encryption? Thanks, Lin
GravityScore #20
Posted 22 March 2013 - 09:19 PM
Sure, go ahead!
Left #21
Posted 22 March 2013 - 10:32 PM
Thanks! Also do I have permission to use it in a little side project. If you don't reply I'll take it as a yes. It saves you the typing. xD
theoriginalbit #22
Posted 22 March 2013 - 10:35 PM
Thanks! Also do I have permission to use it in a little side project. If you don't reply I'll take it as a yes. It saves you the typing. xD
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…).
GravityScore #23
Posted 23 March 2013 - 12:08 AM
Thanks! Also do I have permission to use it in a little side project. If you don't reply I'll take it as a yes. It saves you the typing. xD
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…).

I was going to point this out, but thought nahhh. Linearus: you can use this and other smaller utilities/APIs by me freely without having to ask. If you want to use a program, I wouldn't mind you asking, but it's almost a guaranteed yes.
GravityScore #24
Posted 29 March 2013 - 03:38 AM
I've updated the SHA-256 to fix quite a big bug, and it should now print out exactly what is printed out by PHP and JavaScript. Hope this can make a few people's lives easier!

Beware when updating your program: any stored passwords that were generated using the previous version will be redundant, and will not be comparable with this new version.
Azhf #25
Posted 29 March 2013 - 03:40 AM
Awesome man :D/>
Shazz #26
Posted 19 May 2013 - 12:55 AM
This might be a late post.
The implementation doesn't work for strings with length 55.
If the string's length is 55 then the hashing breaks.
Tested multiple times against PHP's hash function.
johnnic #27
Posted 24 June 2013 - 04:42 PM
Would you mind if I used this function in my OS? I am looking for a secure way to store the passwords, and have the OS as a program ran from shell.
GravityScore #28
Posted 24 June 2013 - 06:52 PM
Sure, go ahead!
oxygencraft #29
Posted 18 November 2015 - 06:59 AM
How do i use this thing?
KingofGamesYami #30
Posted 18 November 2015 - 01:21 PM
How do i use this thing?

Put the code at the top of your program, then use the sha256 function to make hashes.
Creator #31
Posted 18 November 2015 - 02:03 PM

os.loadAPI("sha")
sha.sha256("my string")
H4X0RZ #32
Posted 18 November 2015 - 02:24 PM

os.loadAPI("sha")
sha.sha256("my string")
Nope. This "snippet" has to be included in your own code and can't be loaded as API.
Creator #33
Posted 18 November 2015 - 02:43 PM
Yes, exactly. This is your code.
KingofGamesYami #34
Posted 18 November 2015 - 04:11 PM
Yes, exactly. This is your code.

You misunderstand. The code in the OP uses a local sha256 function, therefor when loaded as an API it will not be exposed and the code you posted will error with attempt to call nil.
Creator #35
Posted 18 November 2015 - 04:15 PM
Yes, exactly. This is your code.

You misunderstand. The code in the OP uses a local sha256 function, therefor when loaded as an API it will not be exposed and the code you posted will error with attempt to call nil.
I use this API with sha256 and there has never been a problem.
Lyqyd #36
Posted 18 November 2015 - 04:26 PM
The point is that you cannot possibly be using this code in the way you suggest without modifying it. All functions in the "API" are declared as local, so nothing would be exposed for use if this was loaded with os.loadAPI.
Creator #37
Posted 18 November 2015 - 04:47 PM
In the paste it is local. What is strange is that I have that API and it is not local nor do I remember unlocalizing it. Well, to make it short, remove the local before sha256 is declared.
H4X0RZ #38
Posted 18 November 2015 - 04:53 PM
In the paste it is local. What is strange is that I have that API and it is not local nor do I remember unlocalizing it. Well, to make it short, remove the local before sha256 is declared.

IMO it would be better to add these functions to your own script because otherwise some malware could modify the hash function so it returns always the same "hash" which would be a big security hole.
TheOddByte #39
Posted 19 November 2015 - 06:30 PM
IMO it would be better to add these functions to your own script because otherwise some malware could modify the hash function so it returns always the same "hash" which would be a big security hole.
Well that only applies if someone has access to the computer, and in my experience there aren't that many sophisticated and advanced viruses in CC, so I'd say that you probably don't have to worry about that.
But it's definitely something to consider ofcourse, but it's still easier to keep it as a separate API IMO.
Wojbie #40
Posted 19 November 2015 - 10:19 PM
Personally i just changed it to this form: http://pastebin.com/UsT5BtHa and stick it near top of my program. Less readable but fits in 2 lines. Inside cc editor its basically invisible and in outside editors you can just hide that line easily and fast with one/two clicks..
Anavrins #41
Posted 19 February 2016 - 09:41 PM
I ended up making my own take at implementing SHA256 in Lua and ended up being around 15x faster.
With some rough speed test I could only get up to 105 hash/sec, my implementation was able to get to 1500 hash/sec.
It's still not much compared to say, gpus, but it's quite something for CC.
I also noticed the same thing as Shazz, that any input that's (64*n)+55 bytes in length will produce the incorrect hash, I didn't go deep into the problem, but I know it's the pre-processing part that produces an incorrect padding.
That bug is not present in my implementation.
Here's the pastebin: 6UV4qfNF
Edited on 19 February 2016 - 08:44 PM
Creator #42
Posted 19 February 2016 - 09:54 PM
I ended up making my own take at implementing SHA256 in Lua and ended up being around 15x faster.
With some rough speed test I could only get up to 105 hash/sec, my implementation was able to get to 1500 hash/sec.
It's still not much compared to say, gpus, but it's quite something for CC.
I also noticed the same thing as Shazz, that any input that's (64*n)+55 bytes in length will produce the incorrect hash, I didn't go deep into the problem, but I know it's the pre-processing part that produces an incorrect padding.
That bug is not present in my implementation.
Here's the pastebin: 6UV4qfNF

Nice. If what you say is true, I might use it in my Delta networking framework.
Anavrins #43
Posted 19 February 2016 - 10:25 PM
Nice. If what you say is true, I might use it in my Delta networking framework.
Here's what I used to test the speed Pvy0zFXt
Usage is "speedtest old" and "speedtest new"
Creator #44
Posted 19 February 2016 - 10:45 PM
Nice. If what you say is true, I might use it in my Delta networking framework.
Here's what I used to test the speed Pvy0zFXt
Usage is "speedtest old" and "speedtest new"

Added it to the framework.
sci4me #45
Posted 20 February 2016 - 01:02 AM
I gotta say, a quick glance at the code, and, it looks great. I am curious to see if your bitop implementations are actually faster than the CC ones however.
Anavrins #46
Posted 24 February 2016 - 02:34 AM
I gotta say, a quick glance at the code, and, it looks great. I am curious to see if your bitop implementations are actually faster than the CC ones however.
Well, whether it's faster or not, they were needed, rrotate is not in CC, and CC's rshift gave incorrect results with numbers near 2^32.

Also, replacing line 136 with local extra = -(len + 9)%64 will fix the padding error in OP's implementation.
Edited on 24 February 2016 - 02:06 AM
Kronix #47
Posted 29 February 2016 - 04:31 AM
How exactly does one go about comparing passwords?
Edited on 29 February 2016 - 03:31 AM
Dragon53535 #48
Posted 29 February 2016 - 06:16 AM
Hash the password and save it. Then when the user inputs their password hash that and compare against the already hashed version, if they're the same then they inputted the same password.
3d6 #49
Posted 29 February 2016 - 09:58 PM
I ended up making my own take at implementing SHA256 in Lua and ended up being around 15x faster. With some rough speed test I could only get up to 105 hash/sec, my implementation was able to get to 1500 hash/sec. It's still not much compared to say, gpus, but it's quite something for CC. I also noticed the same thing as Shazz, that any input that's (64*n)+55 bytes in length will produce the incorrect hash, I didn't go deep into the problem, but I know it's the pre-processing part that produces an incorrect padding. That bug is not present in my implementation. Here's the pastebin: 6UV4qfNF
I made it more compact!

local g=string.gsub sha256=loadstring(g(g(g(g(g(g(g(g('Sa=XbandSb=XbxWSc=XlshiftSd=unpackSe=2^32SYf(g,h)Si=g/2^hSj=i%1Ui-j+j*eVSYk(l,m)Sn=l/2^mUn-n%1VSo={0x6a09e667Tbb67ae85T3c6ef372Ta54ff53aT510e527fT9b05688cT1f83d9abT5be0cd19}Sp={0x428a2f98T71374491Tb5c0fbcfTe9b5dba5T3956c25bT59f111f1T923f82a4Tab1c5ed5Td807aa98T12835b01T243185beT550c7dc3T72be5d74T80deb1feT9bdc06a7Tc19bf174Te49b69c1Tefbe4786T0fc19dc6T240ca1ccT2de92c6fT4a7484aaT5cb0a9dcT76f988daT983e5152Ta831c66dTb00327c8Tbf597fc7Tc6e00bf3Td5a79147T06ca6351T14292967T27b70a85T2e1b2138T4d2c6dfcT53380d13T650a7354T766a0abbT81c2c92eT92722c85Ta2bfe8a1Ta81a664bTc24b8b70Tc76c51a3Td192e819Td6990624Tf40e3585T106aa070T19a4c116T1e376c08T2748774cT34b0bcb5T391c0cb3T4ed8aa4aT5b9cca4fT682e6ff3T748f82eeT78a5636fT84c87814T8cc70208T90befffaTa4506cebTbef9a3f7Tc67178f2}SYq(r,q)if e-1-r[1]<q then r[2]=r[2]+1;r[1]=q-(e-1-r[1])-1 else r[1]=r[1]+qVUrVSYs(t)Su=#t;t[#t+1]=0x80;while#t%64~=56Zt[#t+1]=0VSv=q({0,0},u*8)fWw=2,1,-1Zt[#t+1]=a(k(a(v[w]TFF000000),24)TFF)t[#t+1]=a(k(a(v[w]TFF0000),16)TFF)t[#t+1]=a(k(a(v[w]TFF00),8)TFF)t[#t+1]=a(v[w]TFF)VUtVSYx(y,w)Uc(y[w]W0,24)+c(y[w+1]W0,16)+c(y[w+2]W0,8)+(y[w+3]W0)VSYz(t,w,A)SB={}fWC=1,16ZB[C]=x(t,w+(C-1)*4)VfWC=17,64ZSD=B[C-15]SE=b(b(f(B[C-15],7),f(B[C-15],18)),k(B[C-15],3))SF=b(b(f(B[C-2],17),f(B[C-2],19)),k(B[C-2],10))B[C]=(B[C-16]+E+B[C-7]+F)%eVSG,h,H,I,J,j,K,L=d(A)fWC=1,64ZSM=b(b(f(J,6),f(J,11)),f(J,25))SN=b(a(J,j),a(Xbnot(J),K))SO=(L+M+N+p[C]+B[C])%eSP=b(b(f(G,2),f(G,13)),f(G,22))SQ=b(b(a(G,h),a(G,H)),a(h,H))SR=(P+Q)%e;L,K,j,J,I,H,h,G=K,j,J,(I+O)%e,H,h,G,(O+R)%eVA[1]=(A[1]+G)%e;A[2]=(A[2]+h)%e;A[3]=(A[3]+H)%e;A[4]=(A[4]+I)%e;A[5]=(A[5]+J)%e;A[6]=(A[6]+j)%e;A[7]=(A[7]+K)%e;A[8]=(A[8]+L)%eUAVUY(t)t=t W""t=type(t)=="string"and{t:byte(1,-1)}Wt;t=s(t)SA={d(o)}fWw=1,#t,64ZA=z(t,w,A)VU("%08x"):rep(8):format(d(A))V',"S"," local "),"T",",0x"),"U"," return "),"V"," end "),"W","or "),"X","bit32."),"Y","function "),"Z"," do "))()
Anavrins #50
Posted 01 March 2016 - 02:33 AM
I made it more compact!
Sweet :D/>, can I ask you what you used to compact it?
3d6 #51
Posted 01 March 2016 - 06:15 AM
I made it more compact!
Sweet :D/>, can I ask you what you used to compact it?
Notepad++

It costs 10+n bytes to add a gsub rule, which saves (n-1)*c-(10+n) bytes, where n is the length of the string and c is the number of them present. There are a few more optimizations/gsub rules that could be set, but detecting them would require rigorous calculations and I don't think anyone has made a program like that. Not to mention that we will soon run out of characters to replace things with, and have to settle with two-character replacements, ensuring that there are no accidents along the way.
LoganDark #52
Posted 07 March 2016 - 08:58 PM
I'd be interested if you could implement bcrypt in Lua.
Anavrins #53
Posted 07 March 2016 - 09:16 PM
I'd be interested if you could implement bcrypt in Lua.
I've been actually looking into implementing Blowfish and bcrypt in CC a few days ago, but note that that algorithm is designed to be slow and having it in CC will make it even slower.
Creator #54
Posted 07 March 2016 - 09:50 PM
Anavrins, I am using implementation of the SHA256 algorithm, and am very happy with it. Can I distribute software on the CC forums that uses it?
Anavrins #55
Posted 07 March 2016 - 09:51 PM
Anavrins, I am using implementation of the SHA256 algorithm, and am very happy with it. Can I distribute software on the CC forums that uses it?
Sure go ahead :)/>
Edited on 07 March 2016 - 08:55 PM
LoganDark #56
Posted 07 March 2016 - 11:09 PM
I'd be interested if you could implement bcrypt in Lua.
I've been actually looking into implementing Blowfish and bcrypt in CC a few days ago, but note that that algorithm is designed to be slow and having it in CC will make it even slower.
Wouldn't that be perfect for password systems? It would take quite a bit longer to brute-force it.
Anavrins #57
Posted 07 March 2016 - 11:13 PM
Wouldn't that be perfect for password systems? It would take quite a bit longer to brute-force it.
You have to assume that if someone wants to crack a password that was found in a CC program, he will certainly not crack it from within CC.
Cracking outside of CC will always be faster, so might as well make them as fast as possible for convenient in-game use.
BCrypt has an argument that determine the cost factor, it won't be much before a cost factor get's too costly for CC to be any more useful against offline cracking, compared to SHA2 alone.
Edited on 07 March 2016 - 10:22 PM
LoganDark #58
Posted 07 March 2016 - 11:29 PM
Wouldn't that be perfect for password systems? It would take quite a bit longer to brute-force it.
You have to assume that if someone wants to crack a password that was found in a CC program, he will certainly not crack it from within CC.
Cracking outside of CC will always be faster, so might as well make them as fast as possible for convenient in-game use.
BCrypt has an argument that determine the cost factor, it won't be much before a cost factor get's too costly for CC to be any more useful against offline cracking, compared to SHA2 alone.

Well, outside of CC it would still be slower than SHA256. :P/>
LoganDark #59
Posted 29 March 2016 - 03:03 PM
I ended up making my own take at implementing SHA256 in Lua and ended up being around 15x faster.
With some rough speed test I could only get up to 105 hash/sec, my implementation was able to get to 1500 hash/sec.
It's still not much compared to say, gpus, but it's quite something for CC.
I also noticed the same thing as Shazz, that any input that's (64*n)+55 bytes in length will produce the incorrect hash, I didn't go deep into the problem, but I know it's the pre-processing part that produces an incorrect padding.
That bug is not present in my implementation.
Here's the pastebin: 6UV4qfNF

It seems I'm having a little bit of an error trying to run your SHA algorithm after having an upcoming project of mine mostly finished.

Edit: just using sha256("msg")
Edited on 29 March 2016 - 01:10 PM
Anavrins #60
Posted 29 March 2016 - 03:23 PM

It seems I'm having a little bit of an error trying to run your SHA algorithm after having an upcoming project of mine mostly finished.

Edit: just using sha256("msg")
What CC version are you using, this implies that your using an older version of CC which does not contain bit32.
In any case you can replace the bit32 parts with bit, though I haven't tested it.
Edited on 29 March 2016 - 01:25 PM
LoganDark #61
Posted 29 March 2016 - 03:24 PM
I ended up making my own take at implementing SHA256 in Lua and ended up being around 15x faster.
With some rough speed test I could only get up to 105 hash/sec, my implementation was able to get to 1500 hash/sec.
It's still not much compared to say, gpus, but it's quite something for CC.
I also noticed the same thing as Shazz, that any input that's (64*n)+55 bytes in length will produce the incorrect hash, I didn't go deep into the problem, but I know it's the pre-processing part that produces an incorrect padding.
That bug is not present in my implementation.
Here's the pastebin: 6UV4qfNF

It seems I'm having a little bit of an error trying to run your SHA algorithm after having an upcoming project of mine mostly finished.

Edit: just using sha256("msg")
What CC version are you using, this implies that your using an older version of CC which does not contain bit32.
In any case you can replace the bit32 parts with bit, though I haven't tested it.
Yeah, I bet Mimic is using a different version.

Edit: bit doesn't have a bad lshift
edit 2: I'll copy the functions from the bit32 API and put them in your hashing function. It should fix issues with older versions.
edit 3: not needed, bit does have a blshift but not an lshift. :P/>
edit 4: too long without yielding

Edit 5: I'm using the original API because it works on older versions. It may not be as fast, but that's the compromise you have to make when nobody considers the older versions…
Edited on 29 March 2016 - 01:36 PM
Anavrins #62
Posted 29 March 2016 - 03:41 PM
Yeah, I bet Mimic is using a different version.
Edit: bit doesn't have a bad lshift
Ah yes, as much as I appreciate the work of it's dev, I find that mimic has a lot of issues.
There's nothing wrong with lshift, but definitely with rshift, with number approaching 2^32, I could only get correct results with my own implementation of it.
I ended up modifying the code so that it automatically uses bit if bit32 is not found, it's on the same pastebin link.
If you're getting too long without ending, it's probably your own code, I've tested it working up to inputs of 100000 in length without crashing, just make sure your input isn't this big :P/>
Edited on 29 March 2016 - 01:50 PM
LoganDark #63
Posted 29 March 2016 - 03:49 PM
Yeah, I bet Mimic is using a different version.
Edit: bit doesn't have a bad lshift
Ah yes, as much as I appreciate the work of it's dev, I find that mimic has a lot of issues.
There's nothing wrong with lshift, but definitely with rshift, with number approaching 2^32, I could only get correct results with my own implementation of it.
I ended up modifying the code so that it automatically uses bit if bit32 is not found, it's on the same pastebin link.

Well, that won't be a problem unless someone creates an extremely long password.
Anavrins #64
Posted 29 March 2016 - 04:03 PM
Well, that won't be a problem unless someone creates an extremely long password.
It's not a problem because I'm simply not using it, so you can have passwords as long as you'd like.
Hash functions aren't only made for passwords, if I ever wanted to hash something that contained
a lot of 0xFF, bit.brshift would end up dealing with 0xFFFFFFFF, which is 2^32-1, and would break.
All in all, I was still definitely getting wrong results even when hashing just one letter, so better keep my brshift in there :P/>
Edited on 29 March 2016 - 02:10 PM
Waitdev_ #65
Posted 17 May 2016 - 01:06 PM
It's so awesome that you made this, do you mind if i put this in my new program? I've given you a bit of credit and put a link to this thread, is there anything else that you want me to do or is it fine the way I've done it? Thanks.
LoganDark #66
Posted 17 May 2016 - 06:22 PM
It's so awesome that you made this, do you mind if i put this in my new program? I've given you a bit of credit and put a link to this thread, is there anything else that you want me to do or is it fine the way I've done it? Thanks.

I'm sure he won't mind. I've used it in the first, second and third versions of my password lock with no problem. ( ͡° ͜ʖ ͡°)
Anavrins #67
Posted 17 May 2016 - 07:12 PM
It's so awesome that you made this, do you mind if i put this in my new program? I've given you a bit of credit and put a link to this thread, is there anything else that you want me to do or is it fine the way I've done it? Thanks.
If you're talking about my implementation, then sure, just keep the comment at the top and that should be fine :)/>