Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to encode passwords in PHP

Tags:

php

passwords

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);
like image 695
JasonDavis Avatar asked Sep 08 '09 02:09

JasonDavis


People also ask

What is the best way to encrypt password in PHP?

PHP has a hash algorithm to encrypt the password. The most commonly used functions for password encrypting are md5(), crypt() and password_hash(). Assume we have the registration form data in the POST, which includes the username and password.

Which encryption is best for PHP?

Encryption in PHP is actually simple (we're going to use openssl_encrypt() and openssl_decrypt() once you have made some decisions about how to encrypt your information. Consult openssl_get_cipher_methods() for a list of the methods supported on your system. The best choice is AES in CTR mode: aes-128-ctr.

How can we encrypt the username and password using PHP?

The functions which are generally used to encrypt the username and password in php are md5(), sha1() and base64_encode.


1 Answers

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
like image 112
Rex M Avatar answered Oct 12 '22 09:10

Rex M