Going from unsalted to salted MD5 passwords

后端 未结 12 844
臣服心动
臣服心动 2021-02-07 02:56

I have a LAMP (PHP) website which is becoming popular.

I played it safe by storing the user passwords as md5 hashes.

But I now see that\'s not secure; I should h

相关标签:
12条回答
  • 2021-02-07 03:34

    You can salt them on the fly. Add a piece of code so that, when someone logs in, it does the normal process (computes the MD5 sum of the password and checks it against the stored hash) and if that succeeds, recompute a salted version of the hash from the clear-text password they entered, and store it in the password file.

    The only wrinkle is that you'll need to add an indicator for whether each MD5 is salted or not, since you'll have a mix of both for a while. Or, for a minor loss of security, you can check each password salted and unsalted and if either one hits, accept the login. Of course, if you detect that it was unsalted, then you upgrade at that point.

    0 讨论(0)
  • 2021-02-07 03:35

    Dynamically re-encrypt the passwords when the users log in the next time, i.e. first check whether it’s correct, afterwards encrypt it with a salt and store it again.

    0 讨论(0)
  • 2021-02-07 03:37

    The answer is simple, make sure the keep a record or some sort of flag of which users have passwords on the new system of hashing, when they next login, authenticate them, calculate the new hash, flip the flag.

    Now whenever someone logs in and the flag is set, authenticate them with the new hash.

    0 讨论(0)
  • 2021-02-07 03:41

    A great way to update the passwords while also making them more secure is to change to using a salted SHA1 for passwords. A SHA1 is harder to create a collision against, and it also has a different string length to MD5. A MD5 is 32 characters long, while a SHA1 is 40 characters long.

    To convert these in PHP, you first check the string length of the stored password. If it is 32 characters long, check the password using your old method and afterwards, write a new one using SHA1 to the database.

    If I remember correctly, this is precisely how WordPress handled this issue.

    0 讨论(0)
  • 2021-02-07 03:44

    Salt the original hash as mentioned by others. Just a few pointers here:

    • Salts are better the longer they are. Also if they contain more then just [a-z0-9] but length is better first of all.
    • If someone already has a copy of your DB and you rehash the same passwords with salt, the rehash the old hash with salt will not work. Instead you really should force users to make a new password.
    • You should match new passwords (and passwords to be salted) up against various lists of the most commonly used passwords. These are used in "brute force" attacks. Prompt/force the user to change the password.
    0 讨论(0)
  • 2021-02-07 03:49

    If you're moving away from MD5, you should go skip simply salting and go to an even better technique called stretching. In particular you should use bcrypt (implemented as PHPASS with php).

    Here is a great link on why bcrypt: http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html

    And here is a short How To: 1. Download the phpass package: http://www.openwall.com/phpass/ 2. Look at test.php for examples like the one below:

    require 'PasswordHash.php';
    $t_hasher = new PasswordHash(8, FALSE);
    $correct = 'plaintextpassword';
    $hash = $t_hasher->HashPassword($correct);
    $check = $t_hasher->CheckPassword($correct, $hash);
    

    If $check===true (which is the case above) then the password is correct. If your password is 'hello', you would hash it using HashPassword, put the hash in a database, and when a user logs in, call CheckPassword(userenteredpassword,hashInDb) to see if the password is correct

    0 讨论(0)
提交回复
热议问题