How to create a mySQL user with a hash ('sha256', $ salt. $ Password)?

Something is missing for me.

I want to set up a database user account for transactions only for selection, but mysql does not allow me to choose a hash method for the password when creating the user account.

this fails:

GRANT SELECT ON myDB.* TO 'selectuser'@'localhost' IDENTIFIED BY hash('sha256', 'salted-myfakelongrandompasswordstring'); 

ERROR 1064 (42000): You have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use next to 'hash (' sha256 ',' salted-myfakelongrandompasswordstring ')' on line 1

it goes:

 GRANT SELECT ON myDB.* TO 'selectuser'@'localhost' IDENTIFIED BY 'salted-myfakelongrandompasswordstring'; 

I checked the phpinfo page and the sha256 hash mechanism is already enabled.

Is there a way to change the default hash algorithm for mysql, or is the syntax just wrong in my SQL?

+4
source share
2 answers

No, you should not use your own password hashing for MySQL authentication.

MySQL uses its own password hashing function ( PASSWORD() ), which produces a 41-byte sixth line (based on applying SHA1 to the input twice). Unfortunately, salt is not used.

If you could use GRANT in the form that you specify in your question, then MySQL will apply its PASSWORD() function to the string output of the hash() function. Subsequently, when you want to log in, you will need to enter a 256-bit hash of your password so that it matches what is in the MySQL authentication database.

In addition, MySQL supports the SHA2() family of hash functions with MySQL 6.0.5.

The hash() function is what you probably remember from PHP. It is not part of MySQL.


update: I attended a MySQL conference this week and found out that they are completely changing their roadmap for future product version numbers. The SHA2() function is currently part of the MySQL source, but has not determined which version of the product matches. In addition, you need MySQL built with OpenSSL / YaSSL support for SHA2() to work.


Repeat your comment: Usually, MySQL authentication is completely different from user account authentication in this web application (this is best practice for several reasons).

Yes, you need to hardcode the username / password for MySQL authentication for your web application. Maybe, but a configuration file would be even better. Of course, put them outside the web root.

When the user needs to log in, calculate the hash() their input password, combined with the salt value for the entry for their account. Then compare this with the hash stored in the database for this user. In pseudo code:

 $salt = $db->query("SELECT salt FROM Accounts WHERE account_name = ?", $input_account_name); $password_hash = hash('sha256', $salt + $input_password) $is_password_correct = $db->query("SELECT password_hash = ? FROM Accounts WHERE account_name = ?", $password_hash, $input_account_name); 
+4
source

This page shows that sha256 is not implemented in MySQL:

In addition, with respect to the mentioned exploit from sha1, there are stronger versions like sha256, sha384, sha512, etc., but mysql does not implement them; They must be implemented in code.

+1
source

All Articles