(PHP and MySQL are not my area of expertise, so this might be a simple one-liner).
I just grabbed a piece of code from the Internet that performs user registration. Among other things, it hashes password using these lines:
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$password = hash('sha256', $_POST['password'] . $salt);
for($round = 0; $round < 65536; $round++){ $password = hash('sha256', $password . $salt); }
which, if I understand correctly, is using SHA-256 to store the hash of password and salt values in the password field.
Now I need to write a query that when supplied with the username and password, returns the UserID value. Reading MySQL manual, it looks like there is no built-in function for SHA-256 (old SHA and MD5 are supported). Or is that not the case?
SELECT
id
FROM
users
WHERE
(username = @UN)
AND (password = SHA256(CONCAT(@PWD, salt)))
This obviously doesn't work because SHA256 is not defined.
Edit
Just found out that there is SHA2
in MySQL 5.5.5 that can do SHA-256:
SELECT
id
FROM
users
WHERE
username = @UN
AND password = SHA2(CONCAT(@PWD, salt), 256)
but this also doesn't return any rows. Maybe I'm not doing exactly what the register page above is doing.
Edit 2
I was prbably not clear enough. I'm querying the database from .NET, not PHP. The PHP code I included was just to show how it was being stored in the first place. This means I have to rely on MySQL's capabilities for doing hashing, or do it in .NET maybe, if MySQL can't do it in a query.
Edit 3
I ended up fetching salt value against user name in the first step, then hashing in .NET exactly the same way as done in PHP code above and then passing that to MySQL to get user id.