0

We are migrating a site to Drupal 8. The passwords are hashed with MD5, but Drupal 8 uses a different hashing algorithm.

Which file should I edit to change the hashing system to MD5?

We are aware of the risk, but it has been decided.

5
  • Consider the following approachs to upgrading your hashing system: How to upgrade the hashing method of a live database without compromising security? Commented Jul 11, 2016 at 19:36
  • 1
    The reason to replace the hash algorithm is bad, but the question itself is valid and should not voted down. Especially since Drupal does support password rehasing which allow transparent migration of unsecure hashes. Commented Jul 11, 2016 at 21:10
  • 1
    As a general rule you should never edit Drupal core (or contrib) code to change its behavior. Drupal is extremely flexible and provide extension points for virtually all its behavior. It is usually done though alter hooks, and in Drupal 8, be replacing services with custom one. Commented Jul 11, 2016 at 21:16
  • Perhaps it is not clear how insecure MD5 of SHA1 is. On my laptop I can MD5 or SHA1 over 1,000,000 passwords per second. My ~90% attack: 1. Sort the hashes. 2. Get a 10M password list. 3. For each password run SHA1. 4. Compare each hash to the sorted list of hashes with a binary search, (~17 compares). 5. For each match find the matching record(s). Using my desktop I can perform SHA1 in ~ 1 us.. Time: ~1 us * 10M (passwords) = less than 10 seconds. Nice.
    – zaph
    Commented Jul 11, 2016 at 22:11
  • 1
    Drupal 8 uses sha512 (from the SHA-2 set of hash functions), not SHA1. Drupal uses salt and repeated hashing for passwords (minimum 8 repetitions), see api.drupal.org/api/drupal/… for details. Commented Jul 12, 2016 at 0:42

1 Answer 1

6

In Drupal 8.1+, password hashing and checking is managed by a service that can be replaced in your site's services.yml file. You need to provide your own implementation of PasswordInterface.

Instead of replacing the hash method, it would be safer for your implementation to inherit from PhpassHashedPassword and override the check() method to accept your MD5 hashes (but otherwise use the parent implementation). You can use a dedicated prefix to detect old hashes. And thanks to the PhpassHashedPassword::needsRehash($hash), your password will be securely rehashed over time.

For instance, the following code expect you old hashes to be prefixed with BAD$ in the database.

class MyPasswordService extends PhpassHashedPassword {
  function check ($password, $hash) {
    if (substr($hash, 0, 4) == 'BAD$') {
      // Migrate MD5 has
      $stored_hash = substr($hash, 4);
      $computed_hash = ... old and insecure hash code ...;
      return $computed_hash && Crypt::hashEquals($stored_hash, $computed_hash);
    }
    return parent::check($password, $hash);
  }
}
3
  • SHA1 is essentially no more secure than MD5 for passwords, on my computer there is no real difference in speed, both are over 2,000,000 hashes/sec. So much for "old and insecure hash code". It is really better to replace MD5 with functions such as password_hash, PBKDF2, Bcrypt and etc.
    – zaph
    Commented Jul 11, 2016 at 22:13
  • Drupal 8 uses sha512 (from the SHA-2 set of hash functions), not SHA1. Commented Jul 12, 2016 at 0:33
  • Drupal uses salt and repeated hashing for passwords (minimum 8 repetitions), see api.drupal.org/api/drupal/… for details. Commented Jul 12, 2016 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.