? SHA-256 Hash Function ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password
Space-Restriction Challenge QSP Phase Ends on March 26!

SHA-256 Hash Function

Submitted
Updated
Author
Simeon
Public Key
QKK8Q5GM
Min. Compat.
All
Version
2.2
Size
3.4KB
Rating
8 votes
Description 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
Categories
Keywords
27 Comment(s) Intellis Intellis Out of range in 0:44 : s$="GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"*9999999999999999999999 spaceturtles spaceturtles Video Games I like to play video games! Hobbies Avatar Block I didn't change my avatar for 30 days. Website Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength that long string is definitely out of my range of time I'll use to read it IAmRalsei IAmRalsei Forum Leader Hidden Achievements First Year My account is over 1 year old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Faster version hype!!!! 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself There's already been a faster version for almost a year IAmRalsei IAmRalsei Forum Leader Hidden Achievements First Year My account is over 1 year old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Simeon teased an even faster version Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies Yours doesn't include number bounding, so the numbers overflow to >32 bits and get an error when using HEX$ After adding the bounding, it's much faster than mine still but the outputs are wrong so I probably mistyped something, and will keep debugging. I need to find a better way to bound check, the current solution is as follows: DEF BOUND(N) WHILE N>2147483647 N=N-4294967296 WEND WHILE N<-2147483648 N=N+4294967296 WEND RETURN N END And that's the only solution I've ever got working, the reason N AND &HFFFFFFFF doesn't work is because the bits convert to all 1s when the number exceeds 2147483647. It's kind of buggy in some ways. 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself If you add 2 integers the result will be an integer but only if you use non-constants: X% = 1 ?&H7FFFFFFF + X% '2147483647 + 1 = -2147483648 A lot of people don't seem to know this (I think I wasn't fully aware of it until pretty recently) probably because of confusion when testing it. ?&H7FFFFFFF + 1 is 2147483648, which makes it seem like SB always converts integers to floats when they would overflow. However, the problem is that it's an expression with only constants in it, and SB tries to optimize those during compilation. But for some reason, rather than just computing it normally, SB converts all the values into floats during, does the operations, and then converts the result to an integer IF it can fit within the integer range. So operations done during compilation might not produce the same results as ones done during runtime. EDIT: I just tested it again and it appears to work. Are there certain things which aren't being hashed correctly? Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies Thanks, I used a combination of your code with other code to make it really fast, and remove all the unnecessary functions. I didn't know that integer variables behaved like that, so I just made all the variables Ints and removed the bounding, the performance improved a lot. I didn't think it was fair to convert the string to array outside of the timer, so, everything is within the SHA256 function except the initialization that only gets ran once. 382 hashes a second from hashing "Hello World" repeatedly for 30 seconds. In your code, you can only hash inputs that are >= 4 characters, that part of your code was really obfuscated so I did it my own way. 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Well it's not a string hashing function, it can hash any list of bits. taking a string as input is very limiting since it forces the input to be a multiple of 8 or 16 bits. EDIT: Somehow I never noticed the problem with the string conversion function... For some reason I used CEIL(L DIV 32) when that should be CEIL(L/32)... I'm surprised it didn't cause more problems amihart amihart This is really cool! Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies Thanks! 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Ok, after inlining all the shifts and rotates, the speed is 600 hashes per second Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies Nu uh 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself http://12me21.github.io/sbhighlight3/#Ttextarea%3BP%3B7Nthh~Cbutton MochaProbably MochaProbably Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Night Person I like the quiet night and sleep late. Express Yourself Drawing I like to draw! Hobbies I like how Chrome thinks it’s all in Danish. Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies 12Me21 you should submit it! spaceturtles spaceturtles Video Games I like to play video games! Hobbies Avatar Block I didn't change my avatar for 30 days. Website Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Lol Chrome bugged out for me too. That's a nice program 12. 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Apparently my page stopped working at some point so here's a direct link: https://paste.ee/r/7Nthh 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself I'm getting around 250 per second, with input/output as arrays. Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies 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 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself After moving some of the rotate and shift operations inline, I'm getting around 380 hashes per second. 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself 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. Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies 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 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Hold on I'll make one 3x faster Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies 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... snail_ snail_ Helper Received for being very helpful around SmileBASIC Source Achievements OSP Contest 2 Contest Participant I participated in the second SmileBASIC Source OSP Contest! Night Person I like the quiet night and sleep late. Express Yourself Shove the primes into an inline DATA block inside the function and copy them in. COPY K,@PRIMES 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself You only have to do it once since K is never modified.