![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
Hello I inherited a user database held passwords in clear text. I'm trying to harden the system so I want to store hashes of passwords instead.... Any pointers? Do I really have to do it with PHP? |
#2
| |||
| |||
|
|
Hello I inherited a user database held passwords in clear text. I'm trying to harden the system so I want to store hashes of passwords instead. The user table has few million rows and running a PHP script to do the hashing is the last option. I have this update statement: UPDATE `users` SET `hashedPass` = UNHEX(SHA2(CONCAT(`oldPassword`, 'somesalt'), 256)) I want to store the hashes in a binary column in order to save space (the table could reach few tens of millions of rows). However, the above query is not generating the desired results. After running the query, the hasedPass column contains seemingly random characters - usually few odd characters and sometime completely empty. I think I need to escape the results of the UNHEX function. QUOTE() didn't seem to work. Doing it with php works. $hashed = hash("SHA256", $row['oldPassword']."somesalt", TRUE); $query = "UPDATE `users` SET `hashedPass` = '" . escapeStr($hashed) . "' WHERE `user_id` = {$row['user_id']}"; escapeStr uses mysql_real_escape_string() to return a properly escaped string. and doing a SELECT UNHEX(SHA2(CONCAT(`oldPassword`, 'somesalt'), 256)) returns the same string as the $hashed contents. Only on UPDATE it fails. Any pointers? Do I really have to do it with PHP? TIA |
#3
| |||
| |||
|
|
On Mon, 9 Jul 2012 14:56:52 -0400, Jerry Stuckle wrote (in article <jtf9ho$okp$1 (AT) dont-email (DOT) me>): I wouldn't worry about UNHEX'ing the SHA2 string. You're trading disk space for the additional CPU cycles required to add an additional conversion on every access to the password. Disk space is much cheaper than CPU cycles, even for a few tens of millions of rows. When accessing the records it will be with PHP hashing. the hash function is faster when returning binaries as it doesn't have to re-encode them. So the benefit of unhexing is twofold. |
#4
| |||
| |||
|
|
I have this update statement: UPDATE `users` SET `hashedPass` = UNHEX(SHA2(CONCAT(`oldPassword`, 'somesalt'), 256)) I want to store the hashes in a binary column in order to save space (the table could reach few tens of millions of rows). However, the above query is not generating the desired results. After running the query, the hasedPass column contains seemingly random characters - usually few odd characters and sometime completely empty. |
#5
| |||
| |||
|
|
The user table has few million rows and running a PHP script to do the hashing is the last option. I want to store the hashes in a binary column in order to save space (the table could reach few tens of millions of rows). |
#6
| |||
| |||
|
|
On Tue, 10 Jul 2012 21:01:45 -0400, Denis McMahon wrote (in article <jtij9p$9fn$1 (AT) dont-email (DOT) me>): In other words, the problem may not be the data in "hashedpass" after the update runs, but rather the limitations of the mechanisms you are subsequently using to inspect it! I know how to compare binary data. Anyway, I've solved the problem. The source of the problem turned out to be the character set used to store the binary data. It was set to utf8_bin and that somehow screwed things up. By changing it to BINARY ascii, the results are now consistent. The confusing thing was that when inserting an escaped binary hash (generated by PHP) into the field it worked flawlessly. But when letting Mysql generate the hash, it broke. So it's not utf8_bin's unsuitability for the binary data that was the problem. Something is wrong with mysql's handling of this issue. For everybody else chiming in: Binaries are always, always faster than hex. The only time that binary data is not good is when a human is trying to read it visually. In all other cases, binary data is better and faster (computers calculate in binary, right?). Remember, each time that the system logs in a user, the password data needs to be compared. Which is faster? comparing two 64 char strings or two 32 char strings? Repeat a minimum of a million times per day. Try the hash() function in PHP. When it returns binary data it's faster. Not by much (2%), but faster nontheless. Which is faster: 1: store long hex password hash get password entry generate long hex hash from entry compare two long hex hashes or 2: store short binary password hash get password entry generate short binary hash from entry compare two short binary hashes ? All of you were focused on the 'UNHEX()' function in mysql because the SHA2() function returns hex data only. So to you it's an additional function. But, in reality, every hash function produces a binary hash, and for some, then it hexes it. In PHP, you can make the hash() function skip the hexing of the binary string before it returns it, hence saving time. Every comparison to be made later, whether I do it with PHP or with Mysql will be faster if I'm comparing short binary strings than long hex ones. The only time that Mysql would be doing the hashing (and wasting time hexing and then unhexing the binary hash) is on the initial transfer of passwords from plain text to hashes. After that it's PHP all the way. To show how schizophrenic mysql is about these things, the SHA2() function returns hex in lower case ascii and the HEX() function returns upper case ascii hex. |
#7
| |||
| |||
|
|
All of you were focused on the 'UNHEX()' function in mysql because the SHA2() function returns hex data only. |
#8
| |||
| |||
|
|
On Wed, 11 Jul 2012 16:11:38 -0400, Jerry Stuckle wrote (in article <jtkmlu$ahg$1 (AT) dont-email (DOT) me>): However, it takes longer for PHP to unhex the SHA2 hash than it takes to compare 64 (or even 256) characters. Why would I have to unhex the sha2 hash in PHP? in PHP, you can give it the 'TRUE' option and it would return a binary hash instead of a hexed hash (obviates the need to unhex). When you use the binary return option in the hash() function, in PHP, it is faster. |
|
This is the point you are ignoring - and it is a very pertinent point. You don't seem to have read my post closely enough. I had said that the hash() function in PHP is faster when returning a binary string. So the point is simply wrong. |
#9
| |||
| |||
|
|
Which is faster: 1: store long hex password hash get password entry generate long hex hash from entry compare two long hex hashes or 2: store short binary password hash get password entry generate short binary hash from entry compare two short binary hashes |
#10
| |||
| |||
|
|
He is generating an SHA2 hash, then unhexing it before storing it in his database. When a user signs in, he generates another SHA2 string. |
|
But that is a hex string |
|
I'd like to see the code you used to generate your test. |
![]() |
| Thread Tools | |
| Display Modes | |
| |