LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

SHA-256 Hash Function

Root / Submissions / [.]

SimeonCreated:
Download:QKK8Q5GM
Version:2.2Size:3.4KB
This is a hashing algorithm that can take an input of any length and return a unique 64-character-output in hexadecimal (256 bits). The great thing about this is the feature called the Avalanche Effect, where you can calculate the hash of a huge file of any size (gigabytes, terabytes in size), then change a single bit and recalculate that hash, now both hashes will be entirely different hashes with around half of the bits flipped. Because of this, the SHA-256 algorithm (with salt) is used to store sensitive information in databases across the flat earth. Using the fastest computer in the world, it would take roughly ten times the age of the universe to reverse engineer one of these hashes using the traditional brute force method. So if you're going to have a password system (or something similar), then hash the password and compare it to the hash of what the user types. This will make the password almost guaranteed impossible to crack. That's how programmers work with passwords.

Instructions:

Usage:
DIM H%[8],K%[64] INITSHA256 'One-time initialization

PRINT SHA256("FOO")
PRINT SHA256("BAR")
Prints out: "9520437CE8902EB379A7D8AAA98FC4C94EEB07B6684854868FA6F72BF34B0FD3" "81F5F5515E670645C30C6340FE397157BBD2D42CAA6968FD296A725EC9FAC4ED"
@New_3DS:          382.37 Hashes/Second
@Original_3DS:     100.67 Hashes/Second

Hold on I'll make one 3x faster

Replying to:12Me21
Hold on I'll make one 3x faster
That's a good challenge, if you can make a faster version that'd be fantastic! T[48] is useless, just delete that I was testing if filling the last 48 characters of W make it function the same, it does. W[64] doesn't have to be that long, it can have a length of 16 and still function the same, but I made it 64 to avoid having to check for "SUBSCRIPT OUT OF RANGE" errors. Lots of implementations do that differently. JavaScript uses a length of 16 because indexing negative numbers does not error, just gives undefined (which turns into 0 when applying math), which is perfect behavior and allows JavaScript to run this algorithm really fast because of no bound checking. K is full of 64 constants (prime numbers), and I generate those every time it's ran, now that I think of it, that's most certainly what's making it slow...

In the previous version, if a block in hex starts with zeros, they would be chopped off, returning incorrect less-than-64-character hashes, that's fixed now. Also, sorry 12Me21 but I made lots of performance improvements... There were quite a bit of things slowing it down

I'm getting around 250 per second, with input/output as arrays.

Replying to:12Me21
I'm getting around 250 per second, with input/output as arrays.
Damn Make sure you're timing things right, that'd make each hash around 4 milliseconds, mine hashes at 9-10 milliseconds each I always thought strings and arrays were the exact same thing in SB

Replying to:12Me21
I'm getting around 250 per second, with input/output as arrays.
After moving some of the rotate and shift operations inline, I'm getting around 380 hashes per second.

Replying to:12Me21
I'm getting around 250 per second, with input/output as arrays.
The most annoying part is how SHA uses pretty much ALL right shifts and right rotates, while SB doesn't have an unsigned right shift operator.

Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second

Replying to:12Me21
Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second
Nu uh

Replying to:12Me21
Hold on I'll make one 3x faster
Shove the primes into an inline DATA block inside the function and copy them in.
COPY K,@PRIMES

Replying to:12Me21
Hold on I'll make one 3x faster
You only have to do it once since K is never modified.

Replying to:12Me21
Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second

This is really cool!

Replying to:12Me21
Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second
I like how Chrome thinks itโ€™s all in Danish.

Replying to:12Me21
Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second
12Me21 you should submit it!

Replying to:12Me21
Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second
Lol Chrome bugged out for me too. That's a nice program 12.

Replying to:amihart
This is really cool!
Thanks!

Replying to:12Me21
Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second
Apparently my page stopped working at some point so here's a direct link: https://paste.ee/r/7Nthh

Faster version hype!!!!

Replying to:IAmRalsei
Faster version hype!!!!
There's already been a faster version for almost a year