6

I have a password being passed from my iPhone app to the database via a php script, user.php.

The variable $pass is populated by the following:

$pass = str_replace("'", "", $_REQUEST['pass']);

How can I encrypt this before it's inserted into my database? I've read a little about the different techniques, but looking for the best way to manage this.

Thanks to everyone.

jww
  • 83,594
  • 69
  • 338
  • 732
BigMike
  • 1,063
  • 3
  • 20
  • 33
  • unrelated: use proper input escaping and don't rely on `str_replace` to filter out all apostrophes – knittl Oct 20 '10 at 20:46
  • it's coming from an iPhone app, passed through URL to the script. – BigMike Oct 20 '10 at 20:50
  • Also see Openwall's [Portable PHP password hashing framework](http://www.openwall.com/phpass/) (PHPass). Its hardened against a number of common attacks on user passwords. – jww Oct 11 '14 at 23:34

7 Answers7

7

Use php's crypt library. Md5 is not encryption, it is hashing.

Also, salt your passwords. Why?

Community
  • 1
  • 1
Chris
  • 11,100
  • 13
  • 44
  • 67
7

While the answer below is technically still correct, php has new recommendations with regards to the hashing algorithms to use. Their recommendation, as of php >= 5.5.0, is to use the password_hash and password_verify functions to hash and verify hashed passwords . As an added benefit, these functions automatically include an individualized salt as part of the returned hash, so you don't need to worry about that explicitly.


If you don't care about retrieving the actual password's value (from the database encrypted value), you can run a one-way hash algorithm on it (such as sha1). This function will return a specific length string (hash) which cannot be used to find the original string (theoretically). It is possible that two different strings could create the same hash (called a collision) but this shouldn't be a problem with passwords.
Example: $pass = sha1($_REQUEST['pass']);

One thing, to make it a little more secure is to add a salt to the hash and run the hash function again. This makes it more difficult to generate a password hash maliciously since the salt value is handled server-side only.
Example: $pass = sha1(sha1($_REQUEST['pass']).sha1("mySalt@$#(%"));

2

First, you should create a random user salt. Then you should store that and the password hash in the database.

$salt = md5(unique_id().mt_rand().microtime());
$pass = sha1($salt.$_REQUEST['pass']);

and save the $salt and $pass in the database. Then when they go to login you look up their row and check the hash:

$user = query('SELECT * FROM `user` WHERE username = ?', array($_REQUEST['username']));

if($user)
{
    // If the password they give maches
    if($user->pass === sha1($user->salt. $_REQUEST['pass']))
    {
        // login
    }
    else
    {
        // bad password
    }
}
else
{
    // user not found
}

Creating a user salt for each account insures rainbow tables are useless and anyone that broken into your server would have to brute-force each password.

Xeoncross
  • 50,836
  • 73
  • 238
  • 351
1

Most basic: Hash it with MD5 or SHA1

$newpass = md5($_REQUEST['pass']);

or

$newpass = sha1($_REQUEST['pass']);

Recently I started storing the username hashed as well, so login attempts are secure using only hashed data for comparisons.

You can "salt" the hashes with extra data so if they are compromised, it's value cannot be found (try googling some simple hashed words).. i.e. use a site-wide string just to alter the standard hash like md5("mySiteSalt!!" . $_REQUEST['pass']); or something more advanced.

Fosco
  • 36,451
  • 6
  • 79
  • 100
  • 1
    @Chris For the record, hashing is a form of encryption (albeit one-way). Regardless, MD5 should NEVER be used to store passwords. With today's rainbow tables, it's as good as plaintext. – mattbasta Oct 20 '10 at 19:47
  • Never said hashing is not a form of encryption, I just said md5 is not for encryption. So why are you suggesting to use md5 to "encrypt" the OP's passwords. At least explain in your answer this before creating another user with convoluted sense of security by using md5 for password encryption. – Chris Oct 20 '10 at 19:48
  • @Chris you don't want to encrypt passwords, you want to hash them. I was editing to add salting, which eliminates the rainbow table vulnerability... though I still contend that if someone gets in your database to get the hashes, no encryption is going to save you. I wouldn't "encrypt" passwords because I don't want them, and I don't want there to be a way to reverse them. – Fosco Oct 20 '10 at 19:51
  • 1
    A per password/user salt is a better idea. – middus Oct 20 '10 at 20:23
  • I'm happy with just SHA1 hashing. This isn't a banking system, just a forum. if someone REALLY wants to hack in and get the passwords, not much they can do with them. I'm adding hashing to provide just a little extra security. – BigMike Oct 20 '10 at 21:06
1

Use crypt with some salt. Such as

$user = strip_tags(substr($_REQUEST['user'],0,32));
$plain_pw = strip_tags(substr($_REQUEST['pass'],0,32));

$password = crypt(md5($plain_pw),md5($user));

as on http://www.ibm.com/developerworks/opensource/library/os-php-encrypt/

brian_d
  • 10,694
  • 4
  • 43
  • 72
0

You should use SHA1 to hash your passwords for storage in the database. It's the simplest, yet most effective way to store passwords:

$password = sha1($password);

It's also exceptionally safe. Though the integrity of it is beginning to creep, it's rather easy to upgrade this function to SHA-256 (which is incredibly secure).

mattbasta
  • 12,633
  • 9
  • 41
  • 67
  • SHA1 is too fast. See the link in my answer. – middus Oct 20 '10 at 20:25
  • @middus It certainly is too fast, but it uses 30% more space than MD5, so at least the tables are bigger. Plus, if someone has enough access to your database to gather the hashed passwords, chances are they can steal a great deal of other unencrypted info. SHA1 isn't "great", but it'll do in 95% of scenarios. – mattbasta Oct 20 '10 at 21:21
0

To find out why md5, sha1 and their speedy friends might not be a good idea, you should read the post Enough With The Rainbow Tables: What You Need To Know About Secure Password Schemes by Thomas Ptacek. The gist:

Finally, we learned that if we want to store passwords securely we have three reasonable options: PHK’s MD5 scheme, Provos-Maziere’s Bcrypt scheme, and SRP. We learned that the correct choice is Bcrypt.

Note: it's PHK, not php.

middus
  • 8,977
  • 1
  • 28
  • 33