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.

210<br />

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

}<br />

?><br />

}<br />

* Add the salt to the hash <strong>and</strong> return it<br />

*/<br />

return $salt . sha1($salt . $string);<br />

Creating a Test Method for Salted Hashes<br />

To see how salted hashes work, create a quick test method for _getSaltedHash() called<br />

testSaltedHash(). This will be a public function that calls <strong>and</strong> outputs the values, enabling you to see<br />

how the script functions.<br />

In the Admin class, define the testSaltedHash() method:<br />

<br />

private $_saltLength = 7;<br />

public function __construct($db=NULL, $saltLength=NULL) {...}<br />

public function processLoginForm() {...}<br />

private function _getSaltedHash($string, $salt=NULL) {...}<br />

public function testSaltedHash($string, $salt=NULL)<br />

{<br />

return $this->_getSaltedHash($string, $salt);<br />

}<br />

Next, add a new file called test.php to use this function <strong>and</strong> place it in the public folder<br />

(/public/test.php). Inside this function, call the initialization file, create a new Admin class, <strong>and</strong> output<br />

three hashes of this word: test. Create the first hash with no salt, <strong>and</strong> then sleep for one second to get a<br />

new timestamp. Create the second hash with no salt, <strong>and</strong> then sleep for another second. Finally, create<br />

the third hash using the salt from the second hash. Insert the following code to accomplish this test:<br />

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

Saved successfully!

Ooh no, something went wrong!