Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP crypt() prepend the salt to the hash?

I am looking into building a login system and after reading the php manual when you pass a 2 digit salt to the crypt() function it returns a hash string, and the first 2 digits of the string are the salt that you used.

example:

$salt = "kr";
echo crypt("mysecret",$salt); //returns "kreOI.F7eOQMY"

My first thought was, wouldn't this help someone who is trying to reverse your hash?

I looked up salt on wikipedia which said:

For best security, the salt value is kept secret.

So I do not understand why then would the crypt function return all hashes prepended with the salt value used?

Is there a reason for this? Should this be a security concern?

like image 503
JD Isaacks Avatar asked Jan 26 '11 17:01

JD Isaacks


People also ask

Is crypt () secure?

In short: yes, that value is absolutely safe to store in a database. Save this answer. Show activity on this post. The hash generated by crypt() is specifically intended to be stored.

How does PHP crypt work?

The crypt() function returns a hashed string using DES, Blowfish, or MD5 algorithms. This function behaves different on different operating systems. PHP checks what algorithms are available and what algorithms to use when it is installed. The salt parameter is optional.

What is the purpose of salting hashes?

Password hash salting is when random data – a salt – is used as an additional input to a hash function that hashes a password. The goal of salting is to defend against dictionary attacks or attacks against hashed passwords using a rainbow table.

What is the purpose of adding a salt to the hashing process of a password?

Salt is a cryptographically secure random string that is added to a password before it's hashed, and the salt should be stored with the hash, making it difficult for an attacker to know the original plaintext without having access to both sources.


2 Answers

The author of the Wikipedia article is conflating salt with the idea of search space, implying salt is a way to deter brute force attacks. Security is not improved by confusing these ideas; someone who can't recognize and delineate these two issues is not a credible guide.

The purpose of salt is to thwart pre-computed lookup tables (like a Rainbow table). Salt prevents an attacker from trading "space" for "time." Every bit of salt doubles the storage requirements for a table; a two byte salt makes a big (65536 times) difference, but eight bytes would require non-existent "yottabyte" storage devices for lookup tables.

Assuming that the salt cannot be kept secret encourages better key-strengthening and password selection, and this leads to more secure system.

However, recent recommendations from NIST encourage the use of an additional, secret "salt" (I've seen others call this additional secret "pepper"). One additional iteration of the key derivation can be performed using this secret as a salt. Rather than increasing strength against a pre-computed lookup attack, this round protects against live dictionary attacks. In this way, it's more like the large number of iterations in a good key derivation function.

This secret serves no purpose if stored with the hashed password; it must be managed as a secret, and that could be difficult in a large user database.

Brute force attacks are best prevented by key-strengthening (applying the hash function thousands of times), and password selection rules (require longer passwords, reject blacklisted entries, etc.), but a "pepper" provides an additional layer of defense.

like image 56
erickson Avatar answered Oct 06 '22 01:10

erickson


I should comment that Crypt is not as bad as Marc B makes it sound, and may in fact be the easiest way to good hashes, as long as you don't rely on its weaker schemes like MD5.

See:

How do you use bcrypt for hashing passwords in PHP?

http://uk.php.net/manual/en/function.crypt.php

http://www.openwall.com/phpass/

like image 33
nucc1 Avatar answered Oct 05 '22 23:10

nucc1