Hashing an entire column using sha512
Asked Answered
S

2

8

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

Secretarygeneral answered 27/9, 2012 at 0:11 Comment(0)
W
15

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)

Wherein answered 27/9, 2012 at 0:14 Comment(3)
Thanks G. Unfortunately, I received this error: "SHA2 does not exist"Secretarygeneral
Ah. What version of MySQL are you using? SHA2 is available in 5.5 onwardWherein
That's it then. I'm using 5.0. Thanks again!!Secretarygeneral
F
1

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.

Fabaceous answered 24/8, 2016 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.