Home » Php » php – Storing SHA-512 Hashes in MySQL

php – Storing SHA-512 Hashes in MySQL

Posted by: admin July 12, 2020 Leave a comment

Questions:

I was wondering if I use PHP’s hash() function to generate sha512 hashes how would my MySQL table field look like in-order to be capable of holding the hashed password.

Here is my current MySQL password field layout

char(40)
How to&Answers:

A sha512 hash is represented as a 128 characters-long string.

For example, the following portion of code :

$sha512 = hash('sha512', "Hello, World!");
echo strlen($sha512);

Will give this output :

128

Which means your char(40) is far too small, and that you should use a char(128).

Another solution would be to store it in a binary form, and not a string — which would mean 64 bytes.

But note it might be harder to deal with that representation, in some cases, I suppose.