Beginners: PHP password hashing [duplicate] - php

This question already has answers here:
Secure hash and salt for PHP passwords
(14 answers)
Closed 7 years ago.
i'm new to php password hashing since i'm also a beginner in php coding. I was able to make a simple login form, it requires a username and password. This password will then be saved to a database by Inserting into table using $_POST['password']. However i read that this is unsafe because its a plain text, you need to hash it.
I tried researching about password hashing in php. i found several write-ups but most of it are not clear for a beginner like me. Though i got some basic ideas but can't think on how to implement it.
Some of my questions:
1. How do I hash the password inputted by the user?
2. Once it is hashed, how do i pass it and save it to my database?
3. Is the password will then be saved as a hash (not plain text) in my database? if so, do i need to extend field length in my database to accommodate long hash passwords?
Those are some of my queries which is obviously from a beginner.
I hope someone would enlighten me or show me where to start. I prefer basics so that I can comprehend.
Thank you very much!
EDIT: ok found some answers on the link provided. Thanks for tagging it as duplicate and i'm sorry for that. cheers!!

The most basic is $var = md5($_POST['password']), you may want to use sha but I would recommend that you use SALT :)
For saving it to the database, it is also the same
"INSERT INTO 'tablename' WHERE password = '$var'"
note that md5 is easy to decrypt, this is only to show you how to hash your password.

Related

how to decrypt md5 password in php? [duplicate]

This question already has answers here:
Is it possible to decrypt MD5 hashes?
(24 answers)
Closed 3 years ago.
i am creating one admin panel for my app but the main problem is that when i am displaying password to admin in plain text then it will creates problem ??
i have stored my password in md5 format in php ? how can i able to decrypt that code in plain text in php ?
i have tried several times with every possibilities, but i haven's find any right answer yet now ?
$string ="hello";
$password= md5 ($string);
i expect plain text password which is reverse of encryption, that is decryption
In Theory no you can't. MD5 is ONE WAY hash algorithm. The original string is (lost) throught transformations. The sequrity of MD5 is compromised but you can not "decrypt or reverse" it. You can use a Rainbow Tables and try to find a match. Why you want to see User Password in clear text? The reason of hashing (encryption but without decryption key) is to protect privacy by turning personal information into “for your eyes only”, it's meens only User shoud be know the Password.
Md5 is a hash algorithm, sometimes incorrectly referred to as “one way encryption”. There is no way to get the original string back.
Also, why would you like to show the password in plain text? That can be a serious security issue. The purpose of hashing is to make sure the user writes the same password every time without anyone else knowing what the password is.

I have accidentally used md5 to hash passwords, is it possible to create script to change everyone password? [duplicate]

This question already has answers here:
Is it possible to decrypt MD5 hashes?
(24 answers)
Closed 5 years ago.
Before knowing about just how dangerous md5 is, I have used it to store passwords. Md5 is terrible for security, and can easily be decoded. I now have 70,000 users registered in my database. Big mistake.
Now, since MD5 can be decoded into a string easily I was wondering if it is possible to loop through everyone's password in my MySQL database, decode it, and change it to a much stronger salt hashing system where it cannot be decoded to a string again. Would this be a viable option or even possible? Or is my only solution to do a hard database reset. Prompting users to change passwords would not be a good solution.
No. However, you can work around it, sort of:
Add a new field to your database to hold a second password.
Allow your users to log in as normal, with the MD5 system.
After they have successfully authenticated, you know their password. So now just use password_hash() on it and store it in the new field.
After some amount of time has passed, all active users will have their password encoded both ways.
Remove the MD5 authentication and replace it with password_verify().
Any users that hadn't logged in during the transition period will simply have to reset their password.
Keep the transition period as short as reasonably possible. This will allow your most active users to transition transparently without having to leave your system exposed for too long.
Note -- ultimately, you should have them change their passwords, as the current ones should be considered weak.
Edit for clarification:
You don't necessarily need to make a new password column. Since the hashes generated by password_hash() can be easily differentiated from those generated by md5(), you can simply use a strlen() check to determine which method to use. However, if you made your password field exactly the width of an MD5 hash string, then it's not going to be wide enough to hold the output of password_hash().

Salt / Hash in PHP vs in Database [duplicate]

This question already has answers here:
Secure hash and salt for PHP passwords
(14 answers)
Closed 8 years ago.
Is there an advantage as to where password hash and salt occurs, in PHP vs in a database? It seems having the process occur inside of a database would be the optimal solution; since the web server and the database would only have to exchange the password and not the salt.
It's okay to store the salt in the database. It's an advantage to do so, because you want to use a different random salt per user.
I recommend doing the hashing in the application.
The reason is that if you do the hashing in an SQL expression, and you use query logging on the database server, you might be storing plaintext samples of the user passwords in the query log.
If you're using something better than a simple hash + salt, like PBKDF2, you're going to have to involve PHP at this point AFAIK. So in terms of best location, for me, the best location is in the code because that's where you can do the "best" method of password hashing.

to de-encrypt from the text generate by bin2hex? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP 2-way encryption: I need to store passwords that can be retrieved
I am working with encrypt the password:
php> echo bin2hex(mhash(MHASH_SHA1,'test'));
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
My question is if I have a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 How Can I get back the test.
Are the function to de-encrypt?
your have to understand the diff between a hash function an encryption.
Hashes are one way. You can't convert back. Checking passwords on login usually works by hashing the password from login, too and then just check if hashes are the same.
Using SHA1 it is easy to convert some text into a hash, but going the other way is very, very time consuming, which is one reason why it's good to use for encryption, and in your example passwords.
You may have some luck with this site -
http://www.md5decrypter.co.uk/sha1-decrypt.aspx
It has a list of common hashes and your example 'test' was easily found.
Why do you want to decode the hash output? You can check the password by hash, instead of trying to decode it. By the way, you can't decode a hash, because it loses information when it gets coded. if you want to encrypt/decrypt you should use MCrypt or another encryption class

Getting MD5 codes from database and decoding it in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
I Have md5 encrypted password, how to give the password to user when he uses “Forgot password”?
PHP:How to send the original password to the user when he clicks forgot password which is encrypted by using md5?
I do not know is it the right way to ask this question but I am implementing my own membership script in PHP and right now, I am stuck with the retrieving the MD5 codes from database. OK, I insert the user info to the database and because of the security issues I encrypted the password before saving it to database but my question is that when I try to create a forget your password stuff, how can I get the unencrypted password from the database. By the way I use MySQL and my question is not about inserting or retrieving data from database, I only ask how can I reverse the MD5 thing. Thanks in advance!
You can't. MD5 hashes, or hashes in general, are not reversible. That's exactly the reason why you're using them in the first place to store passwords, because you do not want the responsibility of knowing the actual password.
Forgot password functionality is implemented by sending an email to the user with a one-time link he has to click on and letting him enter a new password.
MD5 was intended to be one-way, but it's now thoroughly insecure. If you're actually serious about having any measure of security, rather than just going through the motions, you have some reading to do:
http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html
SHA512 vs. Blowfish and Bcrypt
http://codahale.com/how-to-safely-store-a-password/
Actually you don't get the password back ever again,
You hash the password entered and compare to the has in your database, thats how it works :) good luck
MD5 is a one-way hash so reversing it wont work.
How you do it is performa comparison against what is stored for example:
SQL for entering the user:
INSERT INTO `users` (`username`, `password`) VALUES ('$username', MD5('$password'));
This will mean that the password is stored as a hash.
When someone tries to log in you do the same thing but in a select statement:
SELECT * FROM `users` WHERE `username` = '$username' AND `password` = MD5('$password');
If there's a result, then the user is authenticated, if there's more than 1 result, then you have fun :)
For the forgot password bit, you are better to set up a chain where the user's are emailed a code and a link. Where they can enter that code on the "password reset" page as well as a new password.
HTH
There are various ways to deal with forgotten passwords, but figuring out the original password from an MD5 hash isn't really one of them.
For the record, however, you really shouldn't be using MD5 for this (or much of anything else related to security). MD5 is pretty badly broken -- unless there's absolutely no choice in the matter, switch to something else (oh, but you should also know that SHA-1 is only a little better than MD5).

Categories