Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    Senior Member
    Join Date
    Apr 2005
    Location
    Hatfield, England
    Posts
    855
    Member #
    9790
    Could someone tell me how to use MD5 to store and retrieve passwords?

    I understand that md5($password) will encrypt the password and return a hash.

    Do i then put that hash into the database?

    When a user inputs a password to login, I would then compare hash's right?

    And finally, to retrieve the password, how would I "unhash" the result to send the password to someone via e-mail?

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    All of that is about right, and the last question shows a misunderstanding of hashes :-) Hashes are one-way functions. You can't get the password back once you've hashed it, that's why it's somewhat more secure than regular encryption: an attacker will *never* be able to crack the hash and retrieve the original password. The price you pay is that you can't, either. Typically, you get around this by reseting their password to something and sending that to them.

  4. #3
    Senior Member
    Join Date
    Apr 2005
    Location
    Hatfield, England
    Posts
    855
    Member #
    9790
    Ah thank you, i wanted to get the answer to my questions before i threw myself into it. Nothing worse than starting with wrong assumptions only to find yourself lost :classic:

  5. #4
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Yes, MD5 and its friends are all one-way hashes. There are of course methods to extract the original word from a hash, but they are obviously harder than a usual two-way encryption. AFAIK, MD5 has several "de-hasher" programs around, so even if they're hashed that doesn't mean they can be safely revealed.

    In fact, it's a mystery whether or not there really is a true "one-way hash".
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    No, the systems that break MD5 don't actually discover your original text, but rather try and duplicate the hash with other text. Basically, because you've only got 128 bits of encoding, there will be collisions. MD5 is good because these collisions are very difficult to predict (i.e., to predict which text will collide with which hash) without brute force methods. Recently (last year or two), a few methods have come up that use grids and such and neat little algorithms to reproduce a hash using different text. This effectively breaks the security of the system using the MD5 checksum, but doesn't necessarily break the security of the password itself.

    As a side note, a good way to get around this (though it can be slower, obviously) is to hash with multiple algorithms and keep track of all of them. It's substantially more difficult to find data that collides with *both* an MD5 hash *and* an SHA1 hash, for example.

  7. #6
    ljm
    ljm is offline
    Senior Member ljm's Avatar
    Join Date
    Aug 2006
    Location
    Manchester, England
    Posts
    284
    Member #
    13684
    Liked
    1 times
    I've always found it best to go over the top with hashing, but since my designs have only been small scale things, the effects of doing so are a lot smaller.

    Generally I'd create a salt, then combine that with the user's password and md5 hash it. After that I'd use SHA1, and it'd be done. To retrieve the password, you repeat the process and match it to what's in the database.

    If you brute force the login script, all this hashing won't make any difference; but if the database is attacked and passwords are stolen, the use of a salt (even a randomly generated one) is very helpful.

    In the end though, there'll still be a way around it. I guess that's the problem with anything on a computer: if people created it, people can destroy it.

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    I've used PHP's mcrypt library for three projects: two at work (and one of which was a near 100% copy of the first) and one in WDF v6: http://us.php.net/mcrypt . For the two work projects, I'm using it to encrypt and retrieve (the latter is necessary for the project; otherwise I would have just used MD5) passwords. For WDF v6, it uses it to obfuscate external links so spammers don't get any SEO benefits from posting their links.

    It's actually a very easy library to use, but there is one annoying caveat: it appears to pad decrypted strings with null characters. The problem is if you actually encrypt something that originally had a null character: trimming it might destroy the original data. If you're encrypting things like passwords and other user-entered strings, it's fine, but encrypting binary data is troublesome.

    A snippet of code from WDF v6:
    PHP Code:
    public function encrypt($str$private_key_name)
            {
                
    $this->init_mcrypt();
                return 
    mcrypt_encrypt(self::ENCRYPTION_CIPHER$this->get_private_key($private_key_name), $str"ecb"$this->mcrypt_iv);
            } 
    ...and the init_mcrypt() method:
    PHP Code:
    protected function init_mcrypt()
            {
                if (
    is_null($this->mcrypt))
                {
                    
    $this->mcrypt mcrypt_module_open(self::ENCRYPTION_CIPHER"""ecb""");
                    
    $this->mcrypt_iv mcrypt_create_iv(mcrypt_enc_get_iv_size($this->mcrypt), MCRYPT_DEV_RANDOM);
                }
            } 
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  9. #8
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    ljm -- also an interesting one. I wasn't actually suggesting SHA1ing the MD5 checksum, but rather keeping separate SHA1 and MD5 hashes. Gentoo currently does three or four different kind of hash checks on every package it downloads to ensure that the package is authentic.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:31 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com