PeAk
PeAk

Reputation: 155

Storing SHA-512 Hashes in MySQL

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)

Upvotes: 8

Views: 12161

Answers (1)

Pascal MARTIN
Pascal MARTIN

Reputation: 400902

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.

Upvotes: 19

Related Questions