0

i'm testing several combinations of sha1 and md5:

<?php
$test = 'fail test';
echo nl2br ("Text: $test\n");
echo nl2br ("md5: ".md5($test)."\nsha1: ".sha1($test)."\nsha1(md5): ".sha1(md5($test))."\nmd5(sha1): ".md5(sha1($test)));
?>

Output:

Text: fail test
md5: 748410d0085967c496d54dd8fcbecc96
sha1: d730125e8cb8576459173655148fb6896ef44c09
sha1(md5): faa3ebeecfec45e509e93e6b245a69e2a78785ea
md5(sha1): b48e89b85c350c91eb302c1de96d4249

Which one better, or maybe user something else ? If yes, what then ?

ZeroSuf3r
  • 1,961
  • 8
  • 25
  • 36
  • I am not sure what you are testing?? – Naftali May 24 '11 at 18:09
  • 4
    Better for what purpose? If you're looking for security, I wouldn't use MD5. Using both doesn't increase security. In some cases, it can in fact decrease it. – Ed Marty May 24 '11 at 18:10
  • maybe topic is not correct, but wanna find solution how to make users pass crack impossible or make it just difficult crack it. – ZeroSuf3r May 24 '11 at 18:12
  • Try reading about salting: http://www.developerfusion.com/article/4679/you-want-salt-with-that/3/ – casablanca May 24 '11 at 18:20
  • 1
    @Ed Marty Technically, wouldn't using both be essentially a per-user salt? – ceejayoz May 24 '11 at 18:20
  • 2
    hash of a hash is more like a global salt. It's not a per-user salt, because the same password will give the same hash for two different users. I don't know if hackers have hash-of-hash look up tables built, but if they do, then using a hash-of-hash would decrease security. There's no reason not to use a proper salted hash. – Matthew May 24 '11 at 19:09

3 Answers3

2

Both of them are cryptographic hash functions that operate 1-way only, the main difference being that MD5 output size is 128 bits whereas SHA-1 is 160 bits. In brief, I don't see they are much different to use despite MD5 is more common these days.

Curiously, I can't really see how md5($text) is different from md5(sha($text)) when they all encrypted to a 32 character-long string, what about md5($text."token") for example?

And, what do you mean by better? Is it more good looking or more security? See bcrypt if you prefer security :) Wikipedia: http://en.wikipedia.org/wiki/Bcrypt

  • They're hash functions, not encryption functions. Encryption implies being able to decrypt them. Barring a fundamental breakthrough math theory or a previously unknown flaw in either hash method, sha1 and md5 are one-way operations. – Marc B May 24 '11 at 18:33
  • I see what you mean :) I know MD5 is defined as a cryptographic hash function, but sometimes I say a verb that describes the action of converting a normal string to a md5 string, I usually say "encrypt it using md5". This seems to be theoretically wrong, but people just say it... Thanks for letting me know anyway. –  May 24 '11 at 18:48
1

You should salt your passwords, ALWAYS. This doesn't stop brute force through a login form but if someone managed to get the details, it would be much harder to crack (rainbow tables would be useless unless they manage to get your salt too)

Essentially, if you adding onto the original data or mangling in a controlled way, it will make security a little better. No-one can ever reverse a hash but they can find other inputs thats match the hash. Mangling the user input will make it harder to login for the hackers.

for example, if a user's pass is 123456, if you add a salt of "salt" to it so it becomes 123456salt, the MD5 of this would be 207acd61a3c1bd506d7e9a4535359f8a. A hacker could crack this to become 123456salt but when it comes to using that on your login form, your code will add salt again and the login will fail.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
Jase
  • 599
  • 3
  • 9
1

Hashing a hash adds no extra security. (In fact, it might make it worse if the person has a hash-of-hash lookup table.)

The best hash will be the one that is computationally the most expensive to perform without any vulnerabilities. I would hash passwords with at least sha-256.

Always hash your passwords with a salted key. This key should be unique per password. It doesn't need to be stored privately. The purpose of a salted password is that the hacker who gained access to your database cannot simply compare the hash with a known list of hashes that correspond to common passwords. Instead, he must try to brute force the password by trying every possible password.

By using a unique salt per password, you guarantee that each hash in the database is different, even if they use the same password.

To salt a password, simply create a random string of characters and append it to the password. Here's a sample hash with a 48-bit salt and sha-256:

function make_password($password)
{
  # random 48-bit salt (8 chars when base64 encoded)
  $salt = base64_encode(pack('S3', mt_rand(0,0xffff), mt_rand(0,0xffff), mt_rand(0, 0xffff)));

  return $salt.hash('sha256', $salt.$password);
}

function check_password($password, $hash)
{
  $salt = substr($hash, 0, 8);
  return hash('sha256', $salt.$password) == substr($hash, 8);
}

$password = 'password';
$hash = make_password('password');

echo $hash."\n";

var_dump(check_password('password', $hash));
var_dump(check_password('wrong', $hash));

Every time you run it, the hash will be different. To validate a password, you select the row where the username matches, and then call check_password($password_from_user, $hash_from_db).

Here's a sample output:

AzrD1jZzc693714a43ad5dfd4106c0a620ef23ff9915070711fa170a6670b8164862b496
bool(true)
bool(false)

You can use a larger salt or a stronger hashing algorithm if you prefer. But at minimum, I would use something like the above.

Matthew
  • 47,584
  • 11
  • 86
  • 98