Monthly Archives: November 2009

SHA-256 in MySQL!

I eventually got sick of having to patch MySQL with the OpenSSL SHA-2 functions every time I updated it. Nevermind that there was a SHA2() function in MySQL 6.0, and that 6.0 got completely yanked, and that no one ever backported it to 5.1 or 5.4… Anyway, it’s very slow (150 seconds to hash a 1MB data block), but it’s not too complex. I hereby place it in the public domain.

This code is intended to be saved to a file which can be fed to the mysql command. It has been tested using MySQL 5.1 and the three reference hashes given by, appendix B. It has also been compared to PHP’s implementation and shown to produce the same message schedules and per-pass a-f values.

Update: Now using bitwise AND instead of MOD for the 32-bit math, which I should’ve done all along. It sped things up slightly, but the real CPU usage is still in the enormous number of SUBSTRING calls.

Update 2: Combined the two loops and saved a bit of unneeded data storage. Shaved another few seconds off the 2-minute runtime.

Update 3: Reformatted slightly to better fit my new blog theme. The indentations are less logically sensible this way, sadly.

Continue reading