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

Hi All, I'm new to perl and I'd like to provide an auth mechanism in perl... any way I can't understand why I have two different digest if I use *STDIN or a String. This is my test code:
#!/user/bin/perl use Digest::MD5 ; my $md5 = Digest::MD5->new; $md5->reset; $md5->addfile(\*STDIN); print $md5->hexdigest , "\n" ; $md5->reset; $md5->add("jhondoe:one_real:jh@ondoe"); print $md5->hexdigest , "\n" ;
and for trying I do:
echo -n "jhondoe:one_real:jh@ondoe" | perl computedigest.pl 10933255f493361481c580259b2a1ab5 f483e2cea20ce9d5891abd77c66a01a6
For me it is impossible to understand. Can someone can explain to me, I use perl 5.8.6 on a i686 linux with Digest 2.36 Thank you. Eltorio

Replies are listed 'Best First'.
Re: Newbie doesn't understand Digest::MD5
by Corion (Patriarch) on Apr 19, 2006 at 12:28 UTC

    My guess is that you're getting bitten by not setting the binary mode on the filehandle you're reading from. This is done easiest by using the binmode function:

    binmode STDIN; $md5->addfile(\*STDIN);

    Another possibility might be that the "echo" program is not outputting what you expect - maybe it is adding some whitespace like newlines even though you told it not to - that would be another thing to check.

      Thanks for your help, I've just tried but binary mode doesn't change anything, but testing with string without @ charcter jhondoe:one_real:jhondoe gives a good result !
      echo -n "jhondoe:one_real:jhondoe" | perl computedigest.pl a20d2f25a66afc8db3befa1b4aca4d14 a20d2f25a66afc8db3befa1b4aca4d14
      Can it be a charcter issue? eltorio

        Yeouch - I didn't spot that error. You're running your code without use strict; and use warnings;, and either of them would have alerted you (and me) to what actually went wrong:

        Perl interpreted the @ondoe in your string as an array variable. That array variable isn't used and hence is empty. You need to use single quotes to prevent variable interpolation by Perl.

        Also, you should really start using warnings and strict, as they prevent typos from growing into hidden, hard to track down bugs.