Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,157,933 members, 7,835,112 topics. Date: Tuesday, 21 May 2024 at 04:47 AM

PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe (1619 Views)

What Is The Best Or Most Secure Way To Hash Passwords In PHP? / Data Encryption With Python-how To Encrypt A Textfile Using Python / Does Nairaland Admin Have Users Login Name And Password Or Just Passwords (2) (3) (4)

(1) (Reply) (Go Down)

PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 8:19am On Feb 24, 2016
:
What is password hashing?
This is the process of preventing clear text storage of passwords in our database by encrypting passwords before being stored in the database. Depending on the method you choose to hash or salt passwords, hacking your database becomes less achievable.


There are different methods in different languages for hashing passwords. In this tutorial I will only show how to do this using the bcrypt algorithm and CRYPT_BLOWFISH to produce the hash.
Good news is that our hash will be crypt() compatible. Safe right? Not md-5 or SHA-1 (I think Facebook uses SHA-2 )

Let me grab a quick coffee before diving into this.
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 8:36am On Feb 24, 2016
Waiting
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by talk2hb1(m): 9:54am On Feb 24, 2016
Waiting for lecturer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 11:04pm On Feb 24, 2016
.
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 11:10pm On Feb 24, 2016
Oops, seems like coffee got me down all day, maybe I meant I was going to do some coffee scripting. Anyways, let's dive right in.

password_hash() uses a one way hashing algorithm to create a new password hash and because of it's crypt() compatibility crypt() password hashes can be used with password_hash()

Example 1:


<?php>
echo password_hash("DanielTheGeek" , PASSWORD_DEFAULT) . "\n"; . //DanielTheGeek can be replaced with a variable
</php>


This will produce a 60 character result because it is bcrypt()

Your echoed output when run should produce a set of jargons not more than 60 characters.

Example 2:

<?php>
$hash_options [
'cost' => 14,
];

echo password_hash ("DanielTheGeek", PASSWORD_BCRYPT, $hash_options)
."\n";
?>

You'll still get some jargons show up when you run that, we just increased the default cost for bcrypt() to 14

Example 3;
Time to add salt to the meal

I don't advice people to use a static salt but a randomly generated one due to possible security breaches. I know big firms in our country that use static salting and have not been hacked probably because people didn't notice. I won't mention names here.


<?php
$hash_options = [
'cost' => 13,
'salt' => mcrypt_create_iv(20, MCRYPT_DEV-URANDOM),
];

echo ("nnamdiosu", PASSWORD_BCRYPT, $hash_options)
."\n" ;
?>


Note: I discovered a nairaland glitch while trying to post this, using my name all through displayed some numbers in my screen repeatedly, I had to change it to nnamdiosu. Try this on your side and give me feedback. This could be dangerous if it is what I think it is.

Try this out while I take another hopefully short break.
Cheers

1 Like

Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by nnamdiosu(m): 8:33am On Feb 25, 2016
DanielTheGeek:
Oops, seems like coffee got me down all day, maybe I meant I was going to do some coffee scripting. Anyways, let's dive right in.

password_hash() uses a one way hashing algorithm to create a new password hash and because of it's crypt() compatibility crypt() password hashes can be used with password_hash()

Example 1:


<?php>
echo password_hash("DanielTheGeek" , PASSWORD_DEFAULT) . "\n"; . //DanielTheGeek can be replaced with a variable
</php>


This will produce a 60 character result because it is bcrypt()

Your echoed output when run should produce a set of jargons not more than 60 characters.

Example 2:

<?php>
$hash_options [
'cost' => 14,
];

echo password_hash ("DanielTheGeek", PASSWORD_BCRYPT, $hash_options)
."\n";
?>

You'll still get some jargons show up when you run that, we just increased the default cost for bcrypt() to 14

Example 3;
Time to add salt to the meal

I don't advice people to use a static salt but a randomly generated one due to possible security breaches. I know big firms in our country that use static salting and have not been hacked probably because people didn't notice. I won't mention names here.


<?php
$hash_options = [
'cost' => 13,
'salt' => mcrypt_create_iv(20, MCRYPT_DEV-URANDOM),
];

echo ("nnamdiosu", PASSWORD_BCRYPT, $hash_options)
."\n" ;
?>


Note: I discovered a nairaland glitch while trying to post this, using my name all through displayed some numbers in my screen repeatedly, I had to change it to nnamdiosu. Try this on your side and give me feedback. This could be dangerous if it is what I think it is.

Try this out while I take another hopefully short break.
Cheers


wait o. bros pardon me. I felt md5 is one of the best way to encrypt passwords? also that glitch wen using ur name for the crypting.....was the hash now a constant character or was it random each time u tried it?
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 8:41am On Feb 25, 2016
Nice thread, spreads mat
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 8:47am On Feb 25, 2016
nnamdiosu:



wait o. bros pardon me. I felt md5 is one of the best way to encrypt passwords? also that glitch wen using ur name for the crypting.....was the hash now a constant character or was it random each time u tried it?

Wow, MD5 is just fast..which has made it the focus point of hackers, besides it is somewhat basic. The presence of senior brothers like crypt() and SHA-2 have made MD5 insecure.

The glitch produced constant characters each time I tried to comment with a fixed length. I want to know if it happened to you too, if it's what I'm thinking then it's a big glitch and a danger too.

Note: I am not an expert in cryptography, I just read a lot.

1 Like

Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 8:55am On Feb 25, 2016
Sorry for that, I was testing something....
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 9:03am On Feb 25, 2016
Keep up the good work dude.

1 Like

Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 9:24am On Feb 25, 2016
donjayzi:
Keep up the good work dude.

Thank you for that.
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 2:30pm On Feb 25, 2016
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by Nobody: 2:37pm On Feb 25, 2016
432410570

That's the number set I got
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by nnamdiosu(m): 8:56am On Mar 20, 2020
Thanks again for this bro
Re: PHP Password Hashing. Encrypt Your Passwords And Keep Your Forms Safe by AdolphBrian: 10:26am On Apr 03, 2020
Hey OP, I use fernet. Is it as good as bcrypt?

(1) (Reply)

Please Nairalanders Help Me Out: IIS 8 Page Keep Appearing When I Load Waec Site / I Am Offering To Work On Two Website For Free To Boost My Portfolio / Andela Nigeria Fellowship Cycle XXIX

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.