Best way to encode passwords in PHP

前端 未结 8 1639
死守一世寂寞
死守一世寂寞 2020-12-07 23:51

I currently use,
base64_encode() to encode a user\'s password, this works well because it allows me to simply use base64decode() to dec

相关标签:
8条回答
  • 2020-12-08 00:23

    As an administrator, you never actually need to recall the password of a user. You simply need to know if a string they've once submitted, is identical to another.

    If a user forgets their password, they don't need to be told their old password, you can simply have them provide a new one.

    Since you don't need to know the actual passwords, using a crytographic hash of the words would seem like a safe way to store them. However, large tables of pre-computed strings have been made to easily do a reverse-lookup of the hash back it's original string. These are called rainbow tables.

    To avoid easy lookup of pre-computed string, you should salt your passwords before hashing them. The salt can be their username prepended, or their user ID postfixed, whatever extra information you have on the user that is permanent that you can easily add to the password during authentication.

    0 讨论(0)
  • 2020-12-08 00:25

    Please, please for the sake of your users do not store their passwords in any reversible format! It doesn't matter if it's Base64 encoded or triple-DES 168-bit encryption - if it is reversible, it is exactly as secure as if you didn't encode it at all.

    No website that has any interest in protecting itself or its users (or has a lick of sense) will send a user their password via e-mail. The only thing we can do that's even remotely close to secure is to send users an email with a unique, one-time-use link that lets them set a new password.

    • Store a hash (bcrypt or PBKDF2) of the password which has been salted
    • Throw away the original password as soon as you've hashed it. Excise it from memory.
    • Always require the user to create their own new password over an SSL channel

    Trying to get by with anything else is honestly just negligence. Let's use a very common scenario used in security discussions:

    User Frederic's email is compromised. This could be from leaving his computer unlocked or using a weak password. Regardless, an unauthorized person has access to his messages. Ideally, this would mean nothing more than some embarrassing love letters read by a stranger. Unfortunately, the unauthorized person discovers a forum will email Frederic's password in plain-text. Like most users, Frederic uses the same password for everything, including his online banking. His username is listed in an email from his bank. Now the situation is very unfortunate.

    Users are placing trust in you when they create a credentials-based relationship with you. Part of that trust is that you will keep those credentials as a secure secret between you and them.

    Related

    A lot of the surrounding issues and ideas have been answered very well on SO:

    • Difference between Hashing a Password and Encrypting it
    • Why is challenge-response approach a poor solution for forgotten passwords?
    • Non-random salt for password hashes
    0 讨论(0)
提交回复
热议问题