107

After all these articles circulating online about md5 exploits, I am considering switching to another hash algorithm. As far as I know it's always been the algorithm of choice among numerous DBAs. Is it that much of a benefit to use MD5 instead of (SHA1, SHA256, SHA384, SHA512), or is it pure performance issue?

What other hash do you recommend (taking into consideration data-bound applications as the platform)? I'm using salted hashes currently (MD5 salted hashes). Please consider both md5 file hashes and password hashes alike.

5
  • 6
    You mentioned salted hashes, does that mean you're talking about password hashing? Password hashing requires different properties from normal hashing, which makes SHA-256 almost as bad as MD5 in this context. Commented Sep 8, 2012 at 18:51
  • I'm using md5 hash to check for critical files integrity before loading them, and salted md5 hashes for passwords. Commented Sep 8, 2012 at 19:00
  • 5
    Neither is good choice, but for completely different reasons. Commented Sep 8, 2012 at 19:01
  • like how much bad, i need to fully understand the situation before recoding the whole part, its a bloody 24 hours at minimum. the code base is like 2K Commented Sep 8, 2012 at 19:03
  • 5
    You probably want to read How to securely hash passwords. I think it's one of the most important questions on this site. Commented Sep 9, 2012 at 20:50

7 Answers 7

129

MD5 for passwords

Using salted md5 for passwords is a bad idea. Not because of MD5's cryptographic weaknesses, but because it's fast. This means that an attacker can try billions of candidate passwords per second on a single GPU.

What you should use are deliberately slow hash constructions, such as scrypt, bcrypt and PBKDF2. Simple salted SHA-2 is not good enough because, like most general purpose hashes, it's fast. Check out How to securely hash passwords? for details on what you should use.

MD5 for file integrity

Using MD5 for file integrity may or may not be a practical problem, depending on your exact usage scenario.

The attacks against MD5 are collision attacks, not pre-image attacks. This means an attacker can produce two files with the same hash, if he has control over both of them. But he can't match the hash of an existing file he didn't influence.

I don't know if the attacks applies to your application, but personally I'd start migrating even if you think it doesn't. It's far too easy to overlook something. Better safe than sorry.

The best solution in this context is SHA-2 (SHA-256) for now. Once SHA-3 gets standardized it will be a good choice too.

14
  • 4
    @sarepta hashcat is harmless compared to ocl-hashcat which runs on a GPU. A single GPU can to over 6 billion combinations per second with that. Commented Sep 8, 2012 at 19:23
  • 1
    A pre-image attack is theoretically possible against MD5, current attacks have a computational complexity of 2^123.4 for full pre-image though.
    – ewanm89
    Commented Sep 8, 2012 at 23:05
  • 4
    Keep in mind that partial pre-image attacks are possible with MD5. You can take an existing file and alter metadata / append junk and generate a collision against a file you generate entirely. That's how the MD5 SSL certificate collision attack works.
    – Polynomial
    Commented Sep 10, 2012 at 6:03
  • 4
    @ewanm89 - 2^123.4 is infeasible (even with billions of GPUs calculating billions of MD5 hashes per second for billions of years). Yes its better than 2^128 by a factor of 24, but the distinction is meaningless for real attacks. (But agree with other reasons to avoid MD5).
    – dr jimbob
    Commented Sep 10, 2012 at 6:24
  • 2
    @Polynomial I wouldn't call that a partial pre-image. It's rather something like a structured collision. Commented Sep 10, 2012 at 9:41
38

To complete @CodesInChaos' answer, MD5 is often used because of Tradition, not because of performance. People who deal with databases are not the same people as those who deal with security. They often see no problem in using weak algorithms (e.g. see the joke of an algorithm that MySQL was using for hashing passwords). They use MD5 because they used to use MD5 and are used to using MD5.

Performance is much more often discussed than measured; and yet, logically, there cannot be a performance issue if there is nothing to measure. Using one core of a basic CPU, you can hash more than 400 MBytes per second with MD5, closer to 300 MB/s with SHA-1, and 150 MB/s with SHA-256. On the other hand, a decent hard disk will yield data at an even lower rate (100 to 120 MB/s would be typical) so the hash function is hardly ever the bottleneck. Consequently, there is no performance issue relatively to hashing in databases.

The usual recommendations, for hash functions, are:

  1. Don't do it. You should not use elementary cryptographic algorithms, but protocols which assemble several algorithms so that they collectively provide some security features (e.g. transfer of data with confidentiality and integrity).

  2. Really, don't do it. For storing passwords (more accurately, password verification tokens), don't make a custom mix of a hash function and salts; use a construction which has been studied specifically for such a use. This normally means bcrypt or PBKDF2.

  3. If a hash function is indeed what does the job, then use SHA-256. Consider using any other function only if some serious problem with SHA-256 (most probably its performance) has been duly detected and measured.

1
  • I see your answer as: "Don't use hash functions directly -- use a larger system such as TLS for data transfer, certificates for authentication, and/or bcrypt or PBKDF2 for password storage." Commented Oct 16, 2019 at 14:05
7

I'm using salted hashes currently (MD5 salted hashes).

If you are salting MD5 hashes, you definitely don't want to be using MD5. It sounds like you need to use PBKDF2 or bcrypt.

As far as I know it's always been the algorithm of choice among numerous DBAs.

That's not a compelling reason.

I have worked with a lot of DBAs that are at least 5 years behind in general technology (not using version control, unformatted perl scripts for everything, etc). They might have been particularly bad DBAs, but I think it comes with the extremely conservative mindset of not changing things.

6

Just to complement the answers already given (most of which are excellent) we now have a real world example of where a data breach (Ashley Madison) lead to the entire password table being leaked. They used bcrypt with a random salt to hash the passwords. A security researcher decided to take those hashes and brute force them. This was the result

As a result of all this, bcrypt is putting Herculean demands on anyone trying to crack the Ashley Madison dump for at least two reasons. First, 4,096 hashing iterations require huge amounts of computing power. In Pierce's case, bcrypt limited the speed of his four-GPU cracking rig to a paltry 156 guesses per second. Second, because bcrypt hashes are salted, his rig must guess the plaintext of each hash one at a time, rather than all in unison.

"Yes, that's right, 156 hashes per second," Pierce wrote. "To someone who's used to cracking MD5 passwords, this looks pretty disappointing, but it's bcrypt, so I'll take what I can get."

Pierce gave up once he passed the 4,000 mark. To run all six million hashes in Pierce's limited pool against the RockYou passwords would have required a whopping 19,493 years, he estimated. With a total 36 million hashed passwords in the Ashley Madison dump, it would have taken 116,958 years to complete the job.

At the end of the day, the only ones he was able to crack were ridiculously simple or common passwords (like "123456").

1
  • 1
    Actually, some of them were available as MD5 hashes (either from an older system or something else, I'm not sure). Those were cracked no problem. Commented Jul 11, 2016 at 19:41
0

Yes MD5 is insecure and so is SHA-1, I recommend using SHA-256 if size of the digest is an issue. Remember, if you store it into a BINARY column, it will take less space that if stored into CHAR. Just make sure it is done properly. MD5 is a about 2.3x faster than SHA-256. More benchmarks are at http://www.cryptopp.com/benchmarks.html

1
  • 9
    Do not use standard hashes for password storage. They're way too fast. PBKDF2 / bcrypt are the way to go.
    – Polynomial
    Commented Sep 10, 2012 at 6:05
0

To put it short it is pretty insecure now because of rainbow tables, Rainbow table is a list of MD5 hashes and their matching strings. So basically i would consider other alternatives such as SHA1

1
  • Rainbow tables apply almost equally to all unsalted hashes. Migrating to SHA1 or SHA2 would do almost nothing. The correct approach for password hashing is using a salt together with a deliberately slow password hashing function such as PBKDF2, bcrypt or scrypt. Even with fast unsalted hashes it's often cheaper to run a new GPU search compared to using a rainbow table. Commented Jun 20, 2013 at 15:15
-1


You are all talking about insecurity, but none of you has given a straight anwser to the question.

I have been working with MD5 and Sha512, both of them where easy to crack once I had put my little hacker expert on it, until it both hit us like a thunder! The most simple and effective way to stop "Mass Attacks" like the ones described above, is setting a limiter of login attemps, meaning :

function checkbrute($user_id, $mysqli) {
 // Get timestamp of current time
   $now = time();
   // All login attempts are counted from the past 2 hours. 
   $valid_attempts = $now - (2 * 60 * 60); 

   if ($stmt = $mysqli->prepare("SELECT time FROM login_attempts WHERE user_id = ? AND time > '$valid_attempts'")) { 
  $stmt->bind_param('i', $user_id); 
  // Execute the prepared query.
  $stmt->execute();
  $stmt->store_result();
  // If there has been more than 5 failed logins
  if($stmt->num_rows > 5) {
     return true;
  } else {
     return false;
  }
}
}


You get my point?
The maximum amount of combinations the hacker wants to try, he is limited to 5 fails per 2 hours. You can even block the user after X attempts.

5
  • 8
    You're not accounting for stolen / improperly discarded backups of user databases or user records retrieved en masse through some exploit (say a SQLi). Your answer is pertaining to hacking live databases through and by the server application that is supposed to be the only one accessing such data, one user account at a time, which - if that was the only risk - could be solved much more elegantly and password hashes wouldn't be needed in the first place. Don't believe me? Ask LinkedIn (among many others) ;)
    – TildalWave
    Commented Jun 20, 2013 at 8:18
  • 4
    1) I explicitly wrote that MD5 and SHA-2 are not secure as password hashes. 2) There are no known attacks on SHA-512 when used properly. It's a cryptographic hash, not a password hash. 3) You're missing the point of password hashes. The point of a password hash is to protect passwords when your database has been leaked. In that scenario a rate limiter doesn't help at all. Commented Jun 20, 2013 at 8:28
  • 4
    FWIW limiting login attempts in this ways just makes it very easy to perform denial of service Commented Dec 17, 2015 at 11:19
  • @EricGrange: Obviously you limit them for each IP or whatever, not in total.
    – anon
    Commented Sep 9, 2017 at 5:01
  • 1
    @Evi1M4chine in the era of IPv6, an attacker can easily (and cheaply) have millions of different IPv6 addresses Commented Sep 28, 2017 at 15:56

You must log in to answer this question.

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