Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing an entire column using sha512

Tags:

mysql

sha512

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

like image 457
user1497265 Avatar asked Sep 27 '12 00:09

user1497265


2 Answers

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)

like image 188
Chris Forrence Avatar answered Nov 05 '22 13:11

Chris Forrence


I hope this is not too late. Even if, maybe someone else will find out this hint:

UPDATE TableName SET Hashed = ENCRYPT('Answer', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))) WHERE Hashed IS NULL;

What it does, it creates sha-512 hash, with it's schema: $6$ from string 'Answer'

If you are using debian, you may also use mkpasswd from package libstring-mkpasswd-perl to generate SHA-512 for you, and update as string.

like image 29
Augustin Ghauratto Avatar answered Nov 05 '22 13:11

Augustin Ghauratto