This question already has answers here:
How to use PHP's password_hash to hash and verify passwords
(5 answers)
Closed 10 months ago.
I want to hash passwords in PHP then send the result to MySQL.
From time to time I might want to use the available hashing functions in PHPMyAdmin to update/reset passwords.
I used to use MD5 for this in the past. As its available in PHP, PHPMyAdmin and in MySQL.
However MD5 is no longer an option due to security concerns.
I looked into SHA1 and that is no longer an option either.
SHA2 does not seem to be available in PHPMyAdmin so that wont work for easy password resetting.
One version of PHPMyAdmin I saw used password_hash but it wasn't available on another server so that is one issue. Another issue is it seems to produce a different output each time the function is run probably because of a random salt.
Is there a password hashing function that I can call from PHP, that will also be available in PHPMyAdmin and in MySQL as well?
Basically what should I replace MD5 with?
Gone are the days when there is a similar hashing function in all 3 areas.
I liked how MD5 was available in all 3 areas being PHP, PHPMyAdmin and MySQL. Since password_hash is the recommended way to hash passwords with PHP I will have to use that.
For resetting passwords in PHPMyAdmin I may allow MD5 hash and have the application update it to php password_hash next time the user logs in similar to how Wordpress did it. [If anyone sees a problem with this method I would like to be made aware]
Related
This question already has answers here:
password_hash returns different value every time
(1 answer)
Using PHP 5.5's password_hash and password_verify function
(4 answers)
Closed 5 years ago.
I have a technical question regarding password_hash() & password_verify().
If I run a sample password through password_hash() many times, I get a different result each time. I guess that’s a Good Thing.
The question is how does password_verify() actually verify the candidate password if the actual hash keeps changing?
I ask this question here because it is PHP related.
For those who think this question is a duplicate:
This question is not a duplicated of the linked questions. I am aware that the value changes, and that password_verify_ works with that.
It is a question of how that happens.
As noted on the manual page for the password_hash() function,
The used algorithm, cost and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included in it. This allows the password_verify() function to verify the hash without needing separate storage for the salt or algorithm information.
When the same inputs - algorithm, cost, salt and password - are fed into the password calculation, the same output will be generated. Thus, the password_verify() takes the algorithm, cost and salt from the original calculation, generates a new hash using the password being tested, and compares the previous result with the newly generated one. If they match, the verification succeeds, otherwise it's an error.
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().
This question already has answers here:
Is it possible to decrypt MD5 hashes?
(24 answers)
Closed 6 years ago.
I have a hashed MD5 password saved in a MySQL database. Is there anyway to find out how many characters were in the original password?
I don't think there is because from what I've read it's difficult to decrypt a hashed password anyway.
Anyway I can do this?
EDIT: Because of the serious amount of backlash!! I'm not interested in decrypting a hash, because I know thats not a great idea from what I've read.
The reason I ask is because.
I am migrating an old system including historic database to a new updated application. All users, many thousands have their password saved in the database MD5, but most with less than 8 characters, so I just wanted to know if there was a way to know if their original password was over 8 characters, then I can bcrypt it or force users to change their password.
But I'll have to force all users to change their passwords by the looks of it
From OP's comment:
i was just purely asking to save forcing all users to ti change their passwords if their password was below a certain character limit – frobak
The answer to this then is to use strlen():
http://php.net/manual/en/function.strlen.php
As for MD5, don't use it it's totally unsafe. A lot of water has gone under the bridge in over 30 years.
Use password_hash():
http://php.net/manual/en/function.password-hash.php
As for decrypting a hash; it can't be done/reversed; that's why it's called a hash and not encrypted.
There are what's called "Rainbow tables":
https://en.wikipedia.org/wiki/Rainbow_table
But I'll have to force all users to change their passwords by the looks of it
Consult the following: Converting md5 password hashes to PHP 5.5 password_hash()
That way you can "hit two posts with one stone".
However, MD5 is 32-length. You will need to increase that to 60+ in order to have the proper length when using password_hash() and as Jay Blanchard stated in his comment, otherwise that may fail "silently" later on when using password_verify().
Nope. You can generate random strings and hash those until you find a match and measure the length of that (this will take an extremely long time, and may actually be incorrect because of collisions, don't do it), but outside of that, it's impossible.
The answer is already given and is worth upvoting, nevertheless I would like to share another view on the problem.
Is it possible to find out the character length?
No actually there is no way to tell the number of characters in the original password from a MD5 hash.
Is it possible to find the passwords with less than 8 characters?
Unfortunately yes. Since cracker tools can brute-force about 20 Giga Md5 per second it is possible to test all 7 character password combinations (a-z A-Z 0-9) in only 3 minutes!
Please note that this is not what I recommend to do, since it would be illegal, it is just to show how unsafe such hashes are. Instead you can calculate a second hash from the stored MD5 hash, I described this in another answer.
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.
I am looking for a way to insert encrypted passwords into a database (MySQL) that I can decrypt later. I've done research and I've came to the conclusion that bcrypt would be the more secure way to store passwords, but then I can't get them back, and it's important that I know their passwords in case I need to login to their system (I don't want to rely on IP authentication).
http://php.net/manual/es/function.mcrypt-cbc.php has some good examples of using a library for encryption on both PHP and PERL, but PERL requires an additional library and PHP needs to be a certain version.
I am looking for a solution that has ability to run on PERL and PHP natively (no additional libraries) with versions that atleast a year old. No PHP 5.3 functions or anything of the like.
The system only has 100 or so users, so there isn't a huge risk of someone even getting access to the database, but just incase I want some kind of protection. If need be, I would be OK with having to add a library to PERL, but I can't really be picky with a PHP library or require PHP version higher than 5.0
If you're using MySQL you may want to look into using mysql functions such AES_ENCRYPT/AES_DECRYPT:
http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html
Using a standard hashing function (e.g. one of the sha versions) does mean that you can't get the password back but it doesn't mean you can't log in to their system.
Just update the password hash in the database with a known one (e.g. update user set password = sha1('password') etc), log in, then update the password back to the old hash. You're in, and their password is back to how it was.
If you're encrypting and decrypting, then the keys will need to be on the server; if you're compromised, the attacker will have access to the keys as well, so you might as well leave the passwords unencrypted if you're not going to hash them.
Just hash the passwords using SHA256 or SHA512. It should be enough. Now, you said you want to know their passwords so you can login into their account. You, as the administrator, should have the ability to login as the user without knowing their passwords.
If you need to login as the user then I am guessing you need to change something? Well, an administrator should be able to change users data without having to be logged in as them...
So I can only say fix your system.