Best way to encode passwords in PHP

I currently use,
base64_encode() to encode a user's password, this works well because it allows me to simply use base64decode() to decode the password to a word and send to there email if they lose there password.

I have been reading up on password though and a lot of people seem to say that you should use sha1() to encode a password. I am all for improving my system's security but if I convert to use shal() then I will not be able to send a user there lost password.

What do YOU use? Can you give me some advice? And is there a way to decod to a readable password to email a user?

As I typed this question I just remebered that some forums do not send you a password when requested but instead send a special link to re-set your password, I am guessing that this is because they are unable to decode your password maybe?

//what I use now
$password_encoded = base64_encode($password);

//what I am considering using
$password_encoded = sha1($password);

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

  • 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.


    You should let a user RESET a password but never RETRIEVE their password. That is why you would want to use a one-way hash (SHA2) instead of a form of encryption that lets you decode it.

    Imagine if you left your email open. I could simply request to retrieve your password for some website, delete the email, and you would never know. On the other hand, if you required me to reset the password instead, the account password would change and the owner would obviously realize that something is wrong. (This is a dumb scenario but the concept is what's important)

    Hashes can be "reversed" by trying all possible combinations of words (or using rainbow tables) until a matching hash is produced. One way to avoid this is to append/prepend the provided password with a salt to make it a very long and unpredictable string. The salt should be a unique string of data unique to the individual's account.

    In PHP there is no SHA2 functon. SHA-2 is a family of hash algorithms, (SHA-256, SHA-384, SHA-512, etc...)

    hash('sha256', 'The quick brown fox jumped over the lazy dog.');
    
    链接地址: http://www.djcxy.com/p/21850.html

    上一篇: Cookies PHP

    下一篇: 在PHP中编码密码的最佳方法