There are functions like MD5 and SHA2 in MySQL which can be used to hash values before putting them into the database, or when searching over values.

As I proposed a possible solution in a comment to this answer to a question, I was told that providing unhashed values to a database is unsafe as an attacker can have access to the database. While I do understand that one could read the data that is already in the tables (it's quite obvious), I fail to understand how could they catch the plain data provided to a request by just having access to the database, since the plaintext is never saved, it's hashed before being put to the database.

I've tried to Google the possible ways for an attacker to do that and did not find anything. Another way to reveal the plaintext is a man-in-the-middle kind of attack, which is prevented by using SSL I believe. So the question is if it's possible if an attacker only has access to the database.

  • 1
    It seems that person that wrote the answer that you linked is under the impression that you are trying to build a 'zero access' system, where the server is untrusted and therefore should have zero access to the user's secrets. Is that the case? If so, then insert into tblNotes (f1dNote) values (sha2('usersecret')) is problematic, because the DBMS sees the user's secret when this query is submitted. A rogue server admin (or attacker that has gained access to the server) could capture the user's secret at that point in time.
    – mti2935
    May 5, 2023 at 10:00
  • @mti2935 well, currently I use a virtual hosting from a rather trusted provider, and all the operations are conducted within this server
    – v_slav.b
    May 5, 2023 at 13:10

1 Answer 1


Database systems can have query logs which contain the sensitive data as plaintext, since the hashing takes place after the query has been processed. Given sufficient privileges, the log can be enabled from the MySQL console while the database system is running. MySQL also supports plugins which make it possible to examine queries before they're executed. This again could give an attacker access to the initial data before hashing.

But even more importantly: None of the basic hash algorithms like MD5 or SHA-2 are suitable for protecting the confidentiality of data, except for very special cases (namely when you're dealing with purely random data which cannot realistically be recovered through a brute-force attack against the hashes).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .