Hashing an entire column using sha512

user1497265 picture user1497265 · Sep 27, 2012 · Viewed 13.2k times · Source

I have a table with three columns named: Question, Answer, Hashed. I want to update the Hashed column with the Answer column hashed using sha512.

I've tried to do the update directly from my MySql database using this syntax, but it didn't work:

UPDATE TableName SET Hashed = SHA512(Answer) WHERE Hashed IS NULL

I know the syntax is wrong but not sure why.

Thanks in advance for your help!

R

Answer

Chris Forrence picture Chris Forrence · Sep 27, 2012

Give this a shot.

UPDATE TableName SET Hashed=SHA2(Answer, 512) WHERE Hashed IS NULL;

Note that this will only work with MySQL 5.5 onward. For versions before 5.5, you'll have to use application code to hash it (PHP to get all the rows, iterate through and hash $row['answer'] to SHA512, then run the UPDATE commands on each) (Source: http://dev.mysql.com/doc/refman/5.5/en//encryption-functions.html#function_sha2)