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

ive been working on some code and it doesnt seem to be working right.
here is the code...
#!/usr/bin/perl -w use strict; use Digest::Perl::MD5 'md5_hex'; my $list = "mil-dic.txt" ; #wordlist my $cmd = 'jonsmith'; # hash to crack getmd5(); sub getmd5 { open (F, $list) || die ("\nCan not open file !"); my @md5 = <F>; close(F); print "\nBruteforcing... please wait.\n\n"; foreach my $md5 (@md5) { chomp $md5; #print $md5."\n"; AddTo($md5); } } sub AddTo { my $check = shift; my $match_1 = md5_hex $check ; my $match_2 = md5_hex $cmd; if ($check =~ $cmd) { print $match_1."\n"; print $check."\n\n"; print $match_2."\n"; print $cmd."\n\n"; } exit(0); if ($match_1 =~ $match_2) { print "\nFound !\t[$check]\n\n"; open (A, ">>found.txt") || die ("\nCan not open file to write to !\ +n"); print A "[$match_2] : [$check]\n"; close(A); } }
the output is...
Bruteforcing... please wait. b2ce97ce53eddef748a19dd6d09c9033 jonsmith 146ecb91572c5330c7d750ed29811a38 jonsmith
as you can see the two dont match.
anyone care to enlighten me?

2006-10-10 Retitled by Arunbear, as per Monastery guidelines
Original title: 'md5_hex'

Replies are listed 'Best First'.
Re: md5_hex gives different values for same string
by davidrw (Prior) on Oct 10, 2006 at 13:33 UTC
    Try changing the debugging from:
    if ($check =~ $cmd) { print $match_1."\n"; print $check."\n\n"; print $match_2."\n"; print $cmd."\n\n"; }
    to:
    use Data::Dumper; print Dumper([ $match_1, $check, $match_2, $cmd ]) if $check eq $cmd;
    Obviously $check isn't just 'jonsmith' and has additional characters from the word-list file in it. Hopefully this will show them. You could also try (on *nix):
    grep jonsmith mil-dic.txt | cat -veT
Re: md5_hex gives different values for same string
by monarch (Priest) on Oct 10, 2006 at 13:02 UTC
    Can you use the eq operator to test that $cmd eq $check instead of $check =~ $cmd. It is possible that $check may contain additional (whitespace) characters and still match $cmd..
      changing =~ to eq does change the outcome but ive used chomp on both $cmd and $check before i use md5_hex and it still doesnt work.
        It would be logical that chomp isn't behaving the way you'd expect. Perhaps use regular expressions that explicitly state what you're trying to do until you're comfortable with how chomp functions. Or even write a function of your own.. e.g.
        sub remove_trailing_newlines( $ ) { my ( $stringref ) = @_; ${$stringref} =~ s/[\r\n]+\z//gs; }
        . Then call it instead of chomp. E.g.
        remove_trailing_newlines( \$cmd ); remove_trailing_newlines( \$check );
        You're supposed to use eq, so just use eq.
Re: md5_hex gives different values for same string
by Persib (Acolyte) on Oct 10, 2006 at 13:46 UTC
    Hi,

    First i tried your script on my Windows XP, and it's ok, then i uploaded this script and mil-dic.txt to my SuSe, i ran the script, and Failed..

    Then i rm -rf mil-dic.txt, make a new one, run the script again, and its works fine

      Did you copy the file mil-dic.txt from winxp to linux? If so, unix newlines (\n) vs. dos newlines (\r\n) might cause problems. The \r characters are seen as part of the line.

      file mil_dic.txt on most linuxes reveals whether you have dos or unix newlines. You can use the program dos2unix to convert your file. Real perl cracks convert command line: on linux,

      perl -i -p -e "s/\r//" mil-dic.txt
      (converting unix to dos newlines on a windows machine won't work this way, though)

      From lots of experience learnt after countless headaches: whenever there is strange file behaviour, look for the newlines.

Re: md5_hex gives different values for same string
by Anonymous Monk on Oct 10, 2006 at 13:26 UTC
    Your script's working fine here