Coplan has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a very short no-frills script to convert an old user information database to another user information database. I don't need anything spectatcular, and I'll likely only use the script once. I'm doing this all on a disconnected box. I say that because I want a quick and easy solution, and security isn't really an issue. I don't really want to load a module just to do this one-time thing.

The old database stores passwords plain text. The new one stores them in MD5 format. I would like to convert my old passwords to the new MD5 standars. Is there a way to do this without a module? Maybe there's a way I can shell to the unix prompt and run a unix command and take that output and use IT as the password for the new database?

I appreciate any and all help. Thanks in advanced.

--Coplan

  • Comment on MD5 password encryption on a no frills script

Replies are listed 'Best First'.
Re: MD5 password encryption on a no frills script
by fokat (Deacon) on Oct 25, 2002 at 05:14 UTC
    By all means, take a look at Digest::MD5 and Crypt::PasswdMD5. The later might be a better choice because it implements a standard MD5-based password hashing function, which has been widely tested.

    Note that you will need modules in order to obtain at least the md5hex or equivalent function. Otherwise, you would need to roll your own code to do the same.

    Security is never a concern until it becomes one.

    Regards.

Re: MD5 password encryption on a no frills script
by grep (Monsignor) on Oct 25, 2002 at 05:18 UTC

    I don't really want to load a module just to do this one-time thing.

    You make this sound like breaking bricks, you're just typing 'use Crypt::PasswdMD5;'

    You have several MD5 modules to choose from you can pick one that is easy and fast to use. The time you invest in looking for a module will most likely save you ten-fold in development, and prevent you from making a serious mistake



    grep
    Mynd you, mønk bites Kan be pretti nasti...
Re: MD5 password encryption on a no frills script
by hardburn (Abbot) on Oct 25, 2002 at 14:48 UTC

    You are really better off using SHA-1 if you can. While MD5 hasn't been completely broken, it has had enough problems over the years that it's better to shy away from it. SHA-1 doesn't suffer from the same problems, and its hash length is longer.

    Oh, and be sure to use salt values :)

      Thanks for the advice. Apparently I need to do a bit more research (I'm FAR from a cryptography expert) since I haven't really heard of SHA-1 before. I'm not exactly sure how MD5 would have problems, but the 160bit hash length of SHA1 vs 128 of MD5 makes sense to me. I'm mainly doing a password authentication type system for a few CGI scripts so speed isn't much of an issue anyway. Unfortunately it looks like there isn't any convinient password module for SHA-1 on CPAN. Luckily I have time to dig into all this a lot farther.
        Pay close attention to wether you will (or think you will) need compatibility with something else. If by any chance, you suspect you would need to generate either .htpasswd files/entries or /etc/passwd files/entries, go the Crypt::PasswdMD5 way.

      Unfortunately, i'm parsing into an existing database which is using the MD5 system. But, should I need to design such a system in the future, I will certainly consider the SHA-1 system. Thanks for your feedback.

      --Coplan

Re: MD5 password encryption on a no frills script
by zigdon (Deacon) on Oct 25, 2002 at 12:24 UTC

    As others have said, you should use a module if you can. However, if you can't, you might be able to use the unix 'md5sum' command - it's available at least on my RH6.2 box. If you do have that command, then something like this would work:

    #!/usr/bin/perl -w use strict; my $md5; while (<>) { chomp; $md5 = `echo $_ | md5sum`; $md5 =~ s/ .*//; print "$_ -> $md5"; }

    -- Dan

Re: MD5 password encryption on a no frills script
by archen (Pilgrim) on Oct 25, 2002 at 14:04 UTC
    Actually I'm doing pretty much the same thing. Right now I'm using the standard crypt funcition (which I think uses DES). Anyone have any references to the strength of MD5 vs. whatever crypt uses?
      The crypt() family of functions suffers from a poor name selection. They actually hash the original password and produce a result that is unique for a given password but that, if something similar to an inverse could exist, would return either a very large or an infinite set of potential passwords. These functions fall within what is called a cryptographically-strong hash (or digest)

      Neither the DES-based crypt() nor the MD5-based crypt() use a straight application of the respective digest algorythm. Rather, they involve complex transforms that deliberatelly "lose bits" from the supplied plaintext (ie, the password). This adds additional roadblocks to recovering the original password.

      The relative superiority of the MD5-based crypt(), stems from the fact that the MD5-based transformation can be applied to passwords of any length. The two common "flavors" of the DES-based crypt() I'm aware of, have limits in the length of the password.

      While it is true that certain "vulnerabilities" have been shown into the MD5 algorythm, it is still far away from being cracked. Note that DES has been considered obsolete since long, yet the passwords protected by DES-based crypt() still need to be cracked using brute force. I would expect the same from the MD5-based one.

      Here is where using a salt string works best, as this forces the eventual attacker to scan a much larger key space. The salt, tipically a set of random bytes concatenated to the original password and stored in-the-clear along with the hashed password, increase significantly the key space. Thanks to it, the attacker cannot count on building a large set of hashes whose passwords are known; He would have to calculate all of them just for you.

      The MD5-based crypt() allows salts of any length.

      Hope this helps.

      According to the FreeBSD Handbook, MD5 should be more secure than DES.

      Hey, if there's code up ^^ there ^^, don't blame me if it doesn't work.

      But today you took me walking, Through a land that we have lost,
      While our children sit at websites, With no access to the cost