SHA1 Hashing Algorithm

If you’re feeling lazy right now to create your own hashing algorithm (like me haha!), here’s one of best built in hashing algorithm with its new feature in PHP5 – SHA1. It uses US Secure Hash Algorithm 1.
I didn’t hear any successful hacking attempts or tools for sha1 yet. (If you have, please pm me or comment to this post! thanks… ) Unlike for MD5 hashing, there are successful hacking attempts discovered.

HOW TO’s*Comments on codes serves as its explanation

Put the following PHP code in your index.php file.

$str = 'dalisay'; // we're gonna hash this string

echo sha1( 'dalisay' ); // to be seen on the page

echo nl2br("nn"); // just some line breaks

// in this part, we're just gonna test if sha1 is working in your server
// the random number and digits is the hash code for string 'dalisay'
// the condition for a login system with sha1 passwords goes like this
if ( sha1( $str ) == '49d8801436095c7a0445a3c53c7d31270223069c' ){
    echo "Yes";
    echo "No";

Step 2: Testing. If you end up something like this, sha1 is working fine.

Final output for the codes above

TIPsIf you’re going to use sha1 for passwords to be stored in the database, make the datatype of your password field to BLOB type, just to make it more secure. :)

Thank you for reading! :)

By Mike Dalisay

I'm Mike Dalisay, a pro web developer since 2010. I love web development. Improving our tutorials and source codes makes me happy. Do you want to suggest an edit to our tutorial? Got something to ask about our source codes? You may use our comments section below or email our team at [email protected]


  1. sha1 is “theoretically” susceptible. But there are no successful attacks reported yet. sha3 is currently under development.

    btw, thanks for commenting :)

  2. Sha1 is theoretically susceptible to collision attack, just like MD5. In practice, it is much less likely. The real solution will be sha3, if they ever figure it out.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top