TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-13-2009, 02:01 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default Having trouble with md5()

I am having trouble understanding the md5() function. Can anyone explain it a little and explain how to implement it into my login script?
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 01-13-2009, 03:09 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The md5() function simply returns a hash of the string that you feed into it. The MD5 hash is usually a 32 character hexadecimal number (a string containing only 0-9 and a-f characters). For an in-depth look at what MD5 does, take a look at the MD5 Wikipedia page but don't get too bogged down in the details as that may confuse you even more.
Salathe is offline  
Reply With Quote
Old 01-13-2009, 03:14 PM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

With md5() you can hash your password. md5 isn't reversible, so you can't reverse the hash back to the original password. Ofcourse there are big databases where you can check a md5 hash against, but when you're also salting the password this won't work.

I feel like md5 together with a salt is generally safe enough to store your passwords. Read more about salts here: Working with Dynamic Cryptography Salts.

So, basically you only have to do:

PHP Code:
define('SALT''y0urs4lth3r3');

$szPassword md5(SALT.'your_password');

// You can put $szPassword in the database now. 
maZtah is offline  
Reply With Quote
Old 02-03-2009, 09:35 PM   #4 (permalink)
The Visitor
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Dog Cow is on a distinguished road
Default

Quote:
Originally Posted by code_junkie View Post
I am having trouble understanding the md5() function. Can anyone explain it a little and explain how to implement it into my login script?
Basically, making an MD5 hash is like making a footprint. It's not actually your foot, but only your foot will match!

In the same case, when a user registers, you make an MD5 hash of his password like this:

Code:
if (isset($_POST['submit']))
{
  // user has submit the registration form

  $password = $_POST['password']; // user's plaintext password as submitted by the form

  // In this example, we will assume the user entered
  // greendog32
  // as his password.

  // Now we make an MD5 hash of this.
  $md5_hash = md5($password);

  // $md5_hash currently looks like this:
  // 742898c83a580b611249ecc2f6cc7a2d

  // More code here would insert this hash into a database
  // The original, plaintext password that the user entered is discarded!

}
Now the thing you have to understand is that with MD5, every possible combination of characters hashes to a completely unique, 32 alphanumeric string. In theory, this is the ideal case, in practice, no.

In example:
the hash of the single letter a is : 0cc175b9c0f1b6a831c399e269772661
and the hash of a similar string, aa is: 4124bc0a9335c27f086f24ba207a4912

Quite different, even though the inputs are similar.

Now, here is part 2! We have the MD5 hash of the user's password stored, which is not the actual password. In theory, there should be only one phrase in the entire world which equals the hash, and that is greendog32.

So, here is what the login script would look like:

Code:
if(isset($_POST['submit']))
{
  // user has submit the login form

  // Here is the plaintext password which the user has entered
  $password = $_POST['password'];

  // Some code here will select the MD5 hash from the database
  $hash_from_database = '742898c83a580b611249ecc2f6cc7a2d';

  // Now what we do here is md5() the password which the user just submitted
  // and see if it equals $hash_from_database

  $hash_from_user = md5($password);

  // Now compare!
  if ($hash_from_user == $hash_from_database)
  {
     echo('You have entered the password correctly! The MD5 hashes matched!');
  }
  else
  {
      echo('You have entered a password other than greendog32. 
      Since the hashes did not match, the password you entered was wrong!');
  }

}
Dog Cow is offline  
Reply With Quote
The Following 2 Users Say Thank You to Dog Cow For This Useful Post:
code_junkie (02-04-2009), Wildhoney (02-03-2009)
Old 02-04-2009, 01:12 AM   #5 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Quote:
Originally Posted by Dog Cow View Post
Basically, making an MD5 hash is like making a footprint. It's not actually your foot, but only your foot will match!

In the same case, when a user registers, you make an MD5 hash of his password like this:

Code:
if (isset($_POST['submit']))
{
  // user has submit the registration form

  $password = $_POST['password']; // user's plaintext password as submitted by the form

  // In this example, we will assume the user entered
  // greendog32
  // as his password.

  // Now we make an MD5 hash of this.
  $md5_hash = md5($password);

  // $md5_hash currently looks like this:
  // 742898c83a580b611249ecc2f6cc7a2d

  // More code here would insert this hash into a database
  // The original, plaintext password that the user entered is discarded!

}
Now the thing you have to understand is that with MD5, every possible combination of characters hashes to a completely unique, 32 alphanumeric string. In theory, this is the ideal case, in practice, no.

In example:
the hash of the single letter a is : 0cc175b9c0f1b6a831c399e269772661
and the hash of a similar string, aa is: 4124bc0a9335c27f086f24ba207a4912

Quite different, even though the inputs are similar.

Now, here is part 2! We have the MD5 hash of the user's password stored, which is not the actual password. In theory, there should be only one phrase in the entire world which equals the hash, and that is greendog32.

So, here is what the login script would look like:

Code:
if(isset($_POST['submit']))
{
  // user has submit the login form

  // Here is the plaintext password which the user has entered
  $password = $_POST['password'];

  // Some code here will select the MD5 hash from the database
  $hash_from_database = '742898c83a580b611249ecc2f6cc7a2d';

  // Now what we do here is md5() the password which the user just submitted
  // and see if it equals $hash_from_database

  $hash_from_user = md5($password);

  // Now compare!
  if ($hash_from_user == $hash_from_database)
  {
     echo('You have entered the password correctly! The MD5 hashes matched!');
  }
  else
  {
      echo('You have entered a password other than greendog32. 
      Since the hashes did not match, the password you entered was wrong!');
  }

}
Thank you for this post. I played with it for a few minute and I think I understand it now.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 02-04-2009, 01:50 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Well explained, Dog Cow. Another encryption method that is similar to MD5, just longer and of course a different algorithm, is SHA1, which is inherent to PHP using the sha1() function. The principle is exactly the same as MD5.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-05-2009, 03:16 PM   #7 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Which would be the best way to secure the login?
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 02-05-2009, 06:41 PM   #8 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

That's quite simple to answer:

MD5 or SHA1?

You could also use a salt, to make it harder to reverse, read it up here:

Cryptography's Sodium Chloride
Sakakuchi is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
having trouble updating with mysql sarmenhb Absolute Beginners 7 11-18-2008 07:49 AM
Having trouble learning MySQL database codes... Aaron Absolute Beginners 24 05-08-2008 07:11 PM
Having trouble with Custom Function "isImage" Orc General 4 12-30-2007 12:19 AM
Trouble with Smarty obolus Absolute Beginners 1 10-11-2007 02:59 AM


All times are GMT. The time now is 09:36 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design