0

I'm currently coding my own CMS and I'm at the state of password...

I want to know if I can md5 a password then sha1 it after?

Like:

$password = md5(sha1(mysql_real_escape_string($_POST['passw'])));
Frederick Marcoux
  • 2,195
  • 1
  • 26
  • 57
  • 2
    You can compute the md5 of the sha1 of any sequence of bytes. Why do you ask? – Frederick Cheung Feb 04 '12 at 18:07
  • I'm asking 'cause I don't want to crash my CMS! So I can? – Frederick Marcoux Feb 04 '12 at 18:08
  • 4
    Just a note, you shouldn't escape passwords, when they're hashed with alphanumeric hashes like md5, sha1, etc, they can't contain any harmful characters - and it may alter the password the user expects. – Dan LaManna Feb 04 '12 at 18:13
  • Are you aware that in your example, you'll get an `MD5` string, not a `SHA1` one? You'll need to swap `md5` and `sha1` around if you want to '`sha1` it after'. – leemeichin Feb 04 '12 at 18:23
  • 1
    @Dan although you latter statement is true, the former is not. First, it depends on the output format and a hash result *may* require escaping. Second, it is always a good habit *not to think* if you have to escape your string or not. It should be unconditional. Just to me sure. – Your Common Sense Feb 04 '12 at 18:33
  • @Col.Shrapnel - By former statement you mean, the alphanumeric hashes bit? – Dan LaManna Feb 04 '12 at 18:40

5 Answers5

6

You can md5 any data you'd like, even if it was hashed before.

It will, however, only increase the risk of collisions because you're now working on a smaller dataset.

What are you trying to achieve?

assafs
  • 161
  • 4
  • I want the best security for my password without 1000 of lines! – Frederick Marcoux Feb 04 '12 at 18:09
  • @FrederickMarcoux you'll get no added security from running a SHA1 password through the weaker MD5 algorithm. – leemeichin Feb 04 '12 at 18:16
  • 2
    The best security comes from the strength of the hash function itself. What you can do however quite easily is use [salts](http://highedwebtech.com/2008/04/25/season-your-passwords-with-some-salt/) to improve the strength of commonly used (i.e. commonly precomputed) hash function values; – hackartist Feb 04 '12 at 18:17
  • 1
    I doubt that a dataset would be really smaller – Your Common Sense Feb 04 '12 at 18:36
  • @YourCommonSense Please clarify if the dataset gets reduced on rehashing or not, as the answer makes a bold claim that risk of collisions will now be increased? –  Sep 30 '14 at 10:18
4

Yes you can. No it doesn't make sense.

The security of chained hash functions is allways equal to or less than the security of the weakest algorithm.

i.e. md5(sha1($something)) is not more secure, than sha1($something): If you manage to break the sha1, you get the md5 for free, as shat($something) and sha1($faked_something) have the same value, and thus md5ing them will not change anything.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
2

Make sure you add a salt in there too, this makes it much harder to use rainbow tables against your customer's/user's passwords.

Something like:

$hashedPassword = sha1(md5($password) . $salt . sha1($salt . $password));

Where salt can be a nice long random string itself, either constant across your application or a salt per contact which is stored with the user too.

Lloyd Watkin
  • 485
  • 4
  • 10
1

You obviously can. I don't see why you couldn't.

If you want better security you should consider something like phpass.

entropid
  • 6,130
  • 5
  • 32
  • 45
1

You can do this, but there's no real benefit to it. If you're running your passwords through md5(), you'll get a bit more security from adding a cryptographic salt.

What is SALT and how do I use it? has more info on that.

The other bit of advice you may hear a lot is to not use MD5. SHA1 is comparatively stronger, and you only need to change your password field in your database to accept a 40 character string.

Community
  • 1
  • 1
leemeichin
  • 3,339
  • 1
  • 24
  • 31