-1

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

frobak
  • 397
  • 2
  • 9
  • 26
  • 3
    No - once the password is hashed it no longer exists in original form. However MD5 should **not** be used for password hashing (I'm looking at you Magento) - especially without salting it first. – CD001 Feb 07 '17 at 16:22
  • It's *technically* possible, but you'd have to spend a lot of resources doing it (basically brute-force it) - `md5` is a "one-way" hash, so it cannot be decrypted. You should go away from using it though, `md5` is old and outdated - don't use it for storing passwords. – Qirel Feb 07 '17 at 16:22
  • 1
    ***"Is there anyway to find out how many characters were in the original password?"*** - well, in order to do that, you would had to include a tiny piece of code that would have captured their original password, which I might add is not good etiquette. – Funk Forty Niner Feb 07 '17 at 16:23
  • @Qirel - actually it kind of isn't - you could get an MD5 collision with "badger" and the entire works of Shakespeare theoretically - there are far more possible permutations of strings than available MD5 values. – CD001 Feb 07 '17 at 16:23
  • At this point, breaking MD5 is so easy that you might as well get decrypt the password itself and then figure out password length. – Dimi Feb 07 '17 at 16:24
  • unfortunately there is no way to do that , cause `md5` is a one way hash password , and i suggest you to move to other hash algorithm other than md5 cause it's no longer recommended – PacMan Feb 07 '17 at 16:24
  • OK, I get it. I can see people are pretty passionate about this not being possible!!. I didn't think it was, 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 Feb 07 '17 at 16:30
  • You could always try all possibilities till you hit one that matches that MD5 – apokryfos Feb 07 '17 at 16:30
  • If there finite numbers of possible hashes, is it not so that one&thesame md5 hash can be generated from diff. inputs? `md5(md5(md5(md5())))` or? – JustOnUnderMillions Feb 07 '17 at 16:33
  • You do not have to force folks to change. First of all, read this: Allow users to use the [passwords / phrases](https://xkcd.com/936/) they desire. [Don't limit passwords.](http://jayblanchard.net/security_fail_passwords.html) – Jay Blanchard Feb 07 '17 at 16:47
  • 1
    Now all you have to do is keep 2 columns, one for the old password hash, and one for the new. When a user logs in you check to see if the have a new hash. If they don't you would use their existing password to create a new hash with PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Feb 07 '17 at 16:50
  • 1
    So it goes like this: user logs in with password, password is confirmed via `md5()`. If `md5()` version exists and newly hashed does not, add newly hashed to new column. If both exist, use the newly hashed column to authenticate user. Does that make sense? – Jay Blanchard Feb 07 '17 at 16:54
  • 1
    In addition: if you insist they use a longer password/passphrase you can read the length of the original password when the user logs in, allow them to login (as above) and popup a message for them encouraging them to update their password with something longer. – Jay Blanchard Feb 07 '17 at 17:27
  • @Eamonn could you please remove/retract your duplicate since it doesn't apply, thank you. People stand to close the question based on that which won't hold true to the real question. – Funk Forty Niner Feb 08 '17 at 01:47
  • I thought it was relevant as you can't measure the length of the string without decrypting the hash, but fair enough. – Eamonn Feb 08 '17 at 09:07

3 Answers3

6

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():

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():

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":

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().

Community
  • 1
  • 1
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
  • 1
    This is the correct answer, the only chance you'll get to measure the password length is when the user inputs it. – Eamonn Feb 07 '17 at 16:44
  • Ahhh. ok so i can do it this way. I can take user input and get string length, and then if entered password matches hashed pw I've got my answer. Thanks – frobak Feb 07 '17 at 16:46
  • @frobak again; [this comment I left you earlier...](http://stackoverflow.com/questions/42094885/is-it-possible-to-find-out-the-character-length-of-a-md5d-password#comment71360138_42094885). However, there is no way for you to find out what their password lengths were from the beginning. You need to implement that method now; you can't go back in time, *as it were*, sorry. – Funk Forty Niner Feb 07 '17 at 16:47
  • @frobak Either that, ^ or force them to change their password and capture the length then and saved in another row. I wouldn't do that though but it's up to you and it can be done. P.s.: You're welcome. – Funk Forty Niner Feb 07 '17 at 16:48
  • Yeah new passwords length etc is fine using validation etc – frobak Feb 07 '17 at 16:50
  • 1
    @frobak Reload my answer, I added something near the bottom about md5 to password_hash(). – Funk Forty Niner Feb 07 '17 at 16:54
3

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.

Eamonn
  • 418
  • 3
  • 11
  • 1
    You need to read the question again: Most particularly: *"Is there anyway to find out how many characters were in the original password?"* – Funk Forty Niner Feb 07 '17 at 16:24
  • 1
    The answer is still correct. I did read it. – Eamonn Feb 07 '17 at 16:24
  • this type of question's been asked many times before. Someone will post a duplicate soon. And that dv I noticed you just got, is not mine. – Funk Forty Niner Feb 07 '17 at 16:25
  • Yes - but because of collisions there's no way of knowing whether the match you've found **is** the original password. – CD001 Feb 07 '17 at 16:25
  • Edited answer and flagged question as duplicate. – Eamonn Feb 07 '17 at 16:26
  • @Eamonn Actually, the real question after all was: *"i was just purely asking to save forcing all users to ti change their passwords if their password was below a certain character limit"* - as per [a comment they left under their question](http://stackoverflow.com/questions/42094885/is-it-possible-to-find-out-the-charater-length-of-a-password-md5d#comment71360468_42094885). – Funk Forty Niner Feb 07 '17 at 16:42
  • Answer is still nope :) – Eamonn Feb 07 '17 at 16:44
  • The chance of collision is minimal. – zaph Feb 07 '17 at 17:37
0

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.

Community
  • 1
  • 1
martinstoeckli
  • 21,405
  • 4
  • 52
  • 78