Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert password hashing from MD5 to SHA?

I've got an old application that has user passwords stored in the database with an MD5 hash. I'd like to replace this with something in the SHA-2 family.

I've thought of two possible ways to accomplish this, but both seem rather clunky.

1) Add a boolean "flag" field. The first time the user authenticates after this, replace the MD5 password hash with the SHA password hash, and set the flag. I can then check the flag to see whether the password hash has been converted.

2) Add a second password field to store the SHA hash. The first time the user authenticates after this, hash the password with SHA and store it in the new field (probably delete their MD5 hash at the same time). Then I can check whether the SHA field has a value; this essentially becomes my flag.

In either case, the MD5 authentication would have to remain in place for some time for any users who log in infrequently. And any users who are no longer active will never be switched to SHA.

Is there a better way to do this?

like image 998
Bruce Alderman Avatar asked Sep 04 '09 20:09

Bruce Alderman


People also ask

Can you convert MD5 to SHA1?

You cannot - they're not different "formats", they're entirely different hash functions.

Can you convert an MD5 hash to SHA256?

As a general rule, MD5 is a hashing function, not an encryption algorithm. It's not possible to recover MD5 encrypted passwords to store them with another method. So, there is no way to directly convert MD5 hashs to their SHA256 equivalent.

Can I decrypt MD5 password?

The MD5 cryptographic algorithm is not reversible i.e. We cannot decrypt a hash value created by the MD5 to get the input back to its original value. So there is no way to decrypt an MD5 password.

Can MD5 hashes be reversed?

MD5 is a cryptographic hashing function, which by definition means that it is only computed in one direction and it is not possible to "reverse" it back to its original form.

How to convert MD5 password to SHA256 in MySQL?

Use SHA2 () to convert the MD5 password to SHA256. It calculates the SHA-2 family of hash functions i.e. SHA-224, SHA-256, SHA-384, and SHA-512). Let us first create a table −

What to do with MD5 Hashs?

Many years ago I developed an asp.net site, implemented Forms Authentication, and stored the user passwords as MD5 hashs. From following basic security news it's pretty obvious that MD5 is no longer useful. I see two possible plans for handling my current users. Copy old users table into new design and hash the current MD5 into SHA-512.

What happens to SHA512 when hashing multiple times?

You lose all of the collision resistance of sha512. If md5 (password1) == md5 (password2), then sha512 (md5 (password1)) == sha512 (md5 (password2)). I'm not a security researcher, but since you're hashing multiple times, I suspect you would actually increase the chances of a collision (chance of md5's colliding + chance of sha + md5 colliding).

How to handle current MD5 users?

From following basic security news it's pretty obvious that MD5 is no longer useful. I see two possible plans for handling my current users. Copy old users table into new design and hash the current MD5 into SHA-512. Then when users login I'll hash their input first as MD5 and then as SHA-512.


2 Answers

Essentially the same, but maybe more elegant than adding extra fields: In the default authentication framwork in Django, the password hashes are stored as strings constructed like this:

hashtype$salt$hash

Hashtype is either sha1 or md5, salt is a random string used to salt the raw password and at last comes the hash itself. Example value:

sha1$a1976$a36cc8cbf81742a8fb52e221aaeab48ed7f58ab4
like image 112
Jørn Schou-Rode Avatar answered Oct 30 '22 16:10

Jørn Schou-Rode


You can convert all your MD5 Strings to SHA1 by rehashing them in your DB if you create your future passwords by first MD5ing them. Checking the passwords requires MD5ing them first also, but i dont think thats a big hit.

php-code (login):

prev: $login = (md5($password) == $storedMd5PasswordHash);

after: $login = (sha1(md5($password)) == $storedSha1PasswordHash);

Works also with salting, got the initial idea from here.

like image 34
wolph Avatar answered Oct 30 '22 17:10

wolph