1

Ok i know this topic is brought up a lot in stackoverflow but they don't underline the answers that im looking for.

I use md5 encryption which i told(it was a while ago when i was a noob at php)was safe but if you look it up on good old google its has encrypted and decryption.

so i started to look other places aka here.

ive heard about all the encryption methods so for example SHA-1,MD5,SHA-2,SHA-256,SHA-512 and so on.

A lot of people say use Bcrypt which im looking over, its that vs SHA-512.

and people say use a random salt and save it in your database which is stupid because say a hacker hacks you database and get the salt of all your passwords so it's a small window to change all the salts before the hacker decrypts all the passwords and go on to other places and try them for instance facebook,google and stackoverflow

So my question is it the safest way doing it like this (encrypting (with SHA-512) and using a random salt which will also be stored in a database) or use a fixed salt which is hard wired into my php code which has the same amount of security as the database random salt.

And i have read a lot of posts on this so i think i know what im talking about and i like to impassive that i have read many posts about this about 20 to be precise.

OH almost forgot and is it safer if you encrypt the password multiple times or is about the same as only one encryption?

Thanks for you'r help on a much over written post

Im Sorry for the people im confusing a bit but i didn't get the point of some other peoples posts and i started rambling on about encryption but i was talking about hashing strings.

sorry for that

ryanc1256
  • 570
  • 6
  • 23
  • 3
    *Hashing* (MD5) is not *encryption*. And don't call something "stupid" when you obviously don't understand it. Whatever you've read, either it wasn't good or you didn't get the point. – deceze Apr 26 '12 at 08:48
  • Read this for starters: http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html – deceze Apr 26 '12 at 08:51
  • https://github.com/ircmaxell/PHP-PasswordLib – PeeHaa Apr 26 '12 at 08:52
  • And this: http://security.stackexchange.com/questions/5605/which-password-hashing-method-should-i-use – deceze Apr 26 '12 at 08:54
  • And this: http://security.stackexchange.com/questions/11717/why-are-hash-functions-one-way-if-i-know-the-algorithm-why-cant-i-calculate-t – deceze Apr 26 '12 at 08:54
  • @deceze i probably didn't get the point, and some of them are not good. But im not starting a argument but when did i say something was stupid? – ryanc1256 Apr 26 '12 at 08:57
  • 2
    *"and people say use a random salt and save it in your database which is stupid because..."* – deceze Apr 26 '12 at 08:58
  • i go that off someone elses comment on another post about this :-P – ryanc1256 Apr 26 '12 at 09:00
  • 2
    @user1285198 tell tell user he is stupid and shouldn't be allowed to develop anything – PeeHaa Apr 26 '12 at 09:00
  • "...google its has encrypted and decryption." FALSE, MD5 cannot be decrypted. – peipst9lker Apr 26 '12 at 09:33
  • Also related: http://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms/4948393#4948393 and http://stackoverflow.com/questions/3559437/many-hash-iterations-append-salt-every-time/3559497#3559497 – ircmaxell Apr 26 '12 at 11:42

5 Answers5

7

I use md5 encryption which i told(it was a while ago when i was a noob at php)was safe but if you look it up on good old google its has encrypted and decryption.

First some nitpicking. It is hashing and not encrypting. Hashing is one way. Now to answer your question: don't use md5() to hash passwords. It's not safe anymore. It has been broken for some years now. Not only has there been collisions found (multiple values which result in the same hash), but md5 can be bruteforced really really fast with any decent GPU.

A lot of people say use Bcrypt which im looking over, its that vs SHA-512

You should use bcrypt. It's the best option for password hashing for now.

and people say use a random salt and save it in your database which is stupid

No it is not stupid. Salting passwords prevents an attacker creating a rainbow table for all your passwords. ircmaxell has created a password lib for your convenience which can be found on GitHub.

Some related articles and Stack Overflow posts:

Update

When PHP 5.5 will be released it will introduce an easy way to correctly encrypt your passwords. It will use bcrypt by default and automatically add a salt to your passwords. When a better algorithm will become available in the future (e.g. scrypt) it will be able to use that. For more information see the RFC about this new feature. It will also have a feature which detects the used algorithm of the currently hashed passwords and automatically can update them when users login to a newer (/ safer) algorithm when available. For implementation examples check out this GitHub gist.

If you are still on an older version of PHP and cannot update there is a pure PHP implementation of the C API available with support of PHP >= 5.3.7. This compat API uses the exact same implementation as the C API.

Note: it would even be better to use the safer scrypt, however up till now PHP doesn't support it. If it does at some point I will update this answer.

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • im not saying salting is stupid im just saying keeping the salt value in your database is kinda a security flaw as someone could get in a take all of the salt values and use them against your system to find the passwords – ryanc1256 Apr 26 '12 at 09:02
  • 2
    @user1285198 no it is not a security flaw. The salt is public and that's fine. The only thing that salting prevents is that the attacker couldn't create a rainbow table of all the passwords at once. But the attacker has to generate a rainbowtable for every salt. – PeeHaa Apr 26 '12 at 09:10
  • 3
    @user The point of a salt is to make it computationally much more expensive (and time consuming) to brute-force a password. Yes, it may take even longer if the attacker didn't have the salt, but with a proper hashing algorithm that's a moot point, since it'll take long enough. And since *your* app will need to have access to the hash as well there's usually little you can do to hide it from an attacker anyway. – deceze Apr 26 '12 at 09:10
  • @user1285198 If the attacker already has database access, then your server is compromised, game over! – Lawrence Cherone Apr 26 '12 at 09:11
  • 2
    @LawrenceCherone no its not. If the attacker get access trough sql injection and the passwords are properly encrypted and salted the password hashes / encrypted password would be useless. – PeeHaa Apr 26 '12 at 09:13
  • Yeah I understand that, I mean access.. 1. attacker gets some kind of shell code on server 2. attacker looks at config files. 3.Attacker has database access. game over! But user passwords are safe because the attacker has deleted them all and is using your server to DDOS some other server ;p – Lawrence Cherone Apr 26 '12 at 09:15
2

You're confusing hashing with encryption.

BluesRockAddict
  • 15,525
  • 3
  • 37
  • 35
  • so what way would be safer though is my question kinda – ryanc1256 Apr 26 '12 at 08:50
  • The safest way would be to do some reading on the subject before going any further. This is a good start: http://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms/4948393#4948393 – BluesRockAddict Apr 26 '12 at 08:54
0

I salt with certain characters from the username as well as a string in the php code. Theoretically they need the code and db to figure it out.

Andy Groff
  • 2,660
  • 1
  • 21
  • 25
  • so in your example you take a part of their username and chuck it into a salt and the (hash/encrypt) it am i right? – ryanc1256 Apr 26 '12 at 08:54
  • yea. I dont know if its good advice or not. Basically it has the advantage of making everything have a different salt that isn't stored entirely or explicitly in the database. – Andy Groff Apr 26 '12 at 08:58
0

A salt is worthwhile as it prevents pre-computation of all possible results. Multiple rounds help slow things down a bit too, but not much if someone happens to gain access to the hashes and have several GPUs at their disposal. To answer the question specifically, if I were you and I could use bcrypt, then I would. If you decide to just SHA-512 hash, use salt and multiple rounds (tens of thousands of rounds).

Also, MD5 and SHA1 hashing, etc. is one-way. The only way to get the original output from the hash is by brute force or sheer luck in guessing it.

0

June 2015 Update:

As @PeeHa stated, PHP 5.5 includes a built in mechanism and a short tutorial / explanation for implementing a hashed password with PHP. You can check it out Password Hashing.

Password hashing is possible using these methods:

  • string password_hash ( string $password , integer $algo [, array $options ] )

password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can be used with password_hash().

  • boolean password_verify ( string $password , string $hash )

Verifies that the given hash matches the given password.

You will probably receive the input from the user so in order to prevent SQL Injection I would recommend using these methods to clear the input:

You can use a prepared statement which allows you to insert user input safely into a n SQL query:

 $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
 $stmt->bindParam(':name', $name);
 $stmt->bindParam(':value', $value);

 $name = 'one';
 $value = 1;
 $stmt->execute();

More information regarding prepared statement in @Theos answer.

Other alternatives are clearing the user input strings:

  • string stripslashes ( string $str )

An example use of stripslashes() is when the PHP directive magic_quotes_gpc is on (it was on by default before PHP 5.4), and you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from an HTML form. stripslashes API

  • string mysqli::escape_string ( string $escapestr )

This function is used to create a legal SQL string that you can use in an SQL statement. The given string is encoded to an escaped SQL string, taking into account the current character set of the connection. real_escape_string API

Community
  • 1
  • 1
Technotronic
  • 8,424
  • 4
  • 40
  • 53