02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

208<br />

CHAPTER 6 ■ PASSWORD PROTECTION SENSITIVE ACTIONS AND AREAS<br />

Improving Security with Salted Hashes<br />

While not bulletproof, adding a salt to your hashing algorithm will make cracking your users' passwords<br />

much more cumbersome for attackers. A salt is a string, either predefined or r<strong>and</strong>om, that is used in<br />

addition to the user input when hashing.<br />

Without using a salt, a password may be hashed like this:<br />

$hash = sha1($password);<br />

To add a r<strong>and</strong>om salt to the preceding hash, you could apply the following this code to it:<br />

$salt = substr(md5(time()), 0, 7); // create a r<strong>and</strong>om salt<br />

$hash = $salt . sha1($salt . $password);<br />

The preceding code generates a r<strong>and</strong>om seven-digit salt. The salt is prepended to the password string<br />

before hashing; this means that even if two users have the same password, their individual password<br />

hashes will be different.<br />

However, in order to reproduce that hash, the salt needs to be available. For this reason, the salt is also<br />

prepended, unencrypted, to the hash. This way, when a user signs in, you’re able to extract the salt from<br />

the hash when it’s retrieved from the database <strong>and</strong> use it to recreate the salted hash of the user’s<br />

password:<br />

$salt = substr($dbhash, 0, 7); // extract salt from stored hash<br />

$hash = $salt . sha1($salt . $_POST['password']);<br />

if ( $dbhash==$hash )<br />

{<br />

echo "Match!";<br />

}<br />

else<br />

{<br />

echo "No match.";<br />

}<br />

Incorporating Salted Hashes <strong>and</strong> Rainbow Tables<br />

By adding a salt, rainbow tables are rendered useless. A new table will need to be generated taking the<br />

salt into account in order to crack user passwords; while this isn’t impossible, it’s time-consuming for the<br />

attacker <strong>and</strong> adds an extra layer of security to your app.<br />

In most applications (especially those that don’t store much in the way of sensitive personal information<br />

such as credit card information), a salted password is deterrent enough to ward off potential attackers.<br />

As an additional countermeasure, it is also advisable to add a check for repeated failed attempts to log in.<br />

This way, an attacker has a finite number of attempts to crack a password before being locked out of the<br />

system. This can also prevent denial of service attacks, or attacks in which a huge volume of requests<br />

are sent in an attempt to overload a site <strong>and</strong> take it offline.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!