MyBB Community Forums

Full Version: Convert passwords in a file to md5?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hi!

I have a file with passwords in plain text. Each line of the file has a single password in plain text.

I need to convert the passwords to md5.

Can I do this with PHP?
This should do the trick:

<?php

$file = 'passwords.txt'; // File containing passwords

$passwords = file($file);
$handle = fopen($file, 'w+');

foreach ($passwords as $password)
{
    fwrite($handle, md5($password)."\n");
}

fclose($handle);

echo 'Done.';

?>

Tested briefly; I'd make a backup of the file as it will overwrite it.
md5 isn't the ultimate protection for passwords though, sha1 is better! But both of them can be decrypted (cracked)

For more info:
What's the difference between MD5 and SHA1?
http://stackoverflow.com/questions/15799...md5-in-php

SHA1 vs MD5:
http://stackoverflow.com/questions/22351...-php-login

Warning: If you are manipulating very sensitive data, or your password file is made public, don't use MD5 nor SHA1, these hashes are both optimized to run quickly, and so can be quickly hacked... Just an example: If PayPal or Facebook were using this technology to protect sensitive data, they would be on the History section of Wikipedia Smile
I'm using a random salt too. I only need a way to make php read the passwords from a file and convert them. Smile

The code posted by Steven works (THANK YOU!!), but why the code is generating different hashes for equal strings?
(2011-09-18, 02:39 AM)Speeder Wrote: [ -> ]The code posted by Steven works (THANK YOU!!), but why the code is generating different hashes for equal strings?
Whoops! I totally forgot about carriage returns in the file too. trim()ing the password should do the trick:

<?php

$file = 'passwords.txt'; // File containing passwords

$passwords = file($file);
$handle = fopen($file, 'w+');

foreach ($passwords as $password)
{
    fwrite($handle, md5(trim($password))."\n");
}

fclose($handle);

echo 'Done.';

?>

Works perfectly for me now. Sorry about that!
Thank you very much! Now it works very well. Smile
(2011-09-18, 05:19 AM)Speeder Wrote: [ -> ]Thank you very much! Now it works very well. Smile
No problem! Glad I could help.
(2011-09-18, 12:57 AM)TheGarfield Wrote: [ -> ]But both of them can be decrypted (cracked)

Give me evidence of someone actually reversing MD5.
(2011-09-19, 12:37 AM)Berlo Wrote: [ -> ]
(2011-09-18, 12:57 AM)TheGarfield Wrote: [ -> ]But both of them can be decrypted (cracked)
Give me evidence of someone actually reversing MD5.
Reversing? No. Bruteforcing or using rainbow tables will do the trick, and there are databases of cracked MD5 hashes available to help.

MD5 should not be used; SHA1 and salts are way better.
@Steven:

The functions file_get_contents and file_put_contents can do what you want, just with less lines.
Pages: 1 2 3 4