in reply to Log's and MD5 Hashes

In the event that someone winds up wanting to do the same thing i just did, well, here is a cheap way to determine if files have been tampered with. Not exactly tripwire... ;) the format of the config file is a line by line filepath to the file itself. And now, for the code:
#!/usr/bin/perl -w use Digest::MD5; open CONFIGFILE, "<config.ini" or die "I cant open the config file."; open RESULTS, ">md5TEMP" or die "I cant write the MD5's temp file."; my $a = 0; my $b = 0; my $change=0; open OLDMD5, "<md5" or die "I cant read the MD5's file."; #Do the MD5 Dance while (<CONFIGFILE>) { chomp; $file = $_; open(FILE, $file) or die "Can't open $file"; binmode(FILE); $md5 = Digest::MD5->new; while (<FILE>) { $md5->add($_); } close(FILE); print RESULTS $md5->b64digest, " ", $file, "\n"; #print $md5->b64digest, "\n"; $a=$a+1; } close RESULTS; close CONFIGFILE; open RESULTS, "<md5TEMP" or die "I cant read the MD5's temp file."; my @file1 = <RESULTS>; my @file2 = <OLDMD5>; close OLDMD5; #See what's different while ($a != $b) { if ($file1[$b] ne $file2[$b]) { print $file1[$b]; $change=1; } $b=$b+1; } #If they differ, then update the oldMD5 file to the newest one. $b=0; if ($change eq 1) { open OLDMD5, ">md5" or die "I cant write the MD5's file."; while ($a != $b) { print OLDMD5 $file1[$b]; $b=$b+1; } } close RESULTS; close OLDMD5;

I know it can be cleaner, but hey, I'm new at this ;)

Replies are listed 'Best First'.
Re: Log's and MD5 Hashes -- FINALLY DONE
by Zaxo (Archbishop) on Jan 31, 2002 at 08:09 UTC

    Glad you posted this before I replied to the other, you've fixed this up well. There are still some warts, though.

    1. Don't use $a and $b for scratch variables; they are sacred to sort.
    2. Lock your files or use a locked semaphore file. You have races if more than one instance of this runs (e.g. if it is fired by suspicious tcp connections)
    3. When you die, put $! in die's argument list (without any "\n"). That will give you diagnostics you might not get otherwise.
    4. grep can clean up your last while loop, and maybe help the logic, too.
    5. use strict; use warnings;
    6. Take a look at the &Digest::MD5::addfile(\*HANDLE) method
    7. It will take O(N2) to compare all those digests the way you do it. How about making a hash with the digest as key, and checking for existance in the hash?
    You can turn this into a thing of beauty. Good luck, and have fun.

    After Compline,
    Zaxo

Re: Log's and MD5 Hashes -- FINALLY DONE
by Anonymous Monk on Jan 31, 2002 at 08:24 UTC
    Firstly, you really should be posting comments and follow-up code under the same thread rather than starting new threads for each new discussion on the thread.

    With the code here ...

    while (<CONFIGFILE>) { chomp; $file = $_; open(FILE, $file) or die "Can't open $file"; binmode(FILE); $md5 = Digest::MD5->new; while (<FILE>) { $md5->add($_); } close(FILE); print RESULTS $md5->b64digest, " ", $file, "\n"; #print $md5->b64digest, "\n"; $a=$a+1; }

    ... you could write this a lot more neatly making use of the addfile and reset methods of Digest::MD5 rather than iterating through each line of the file and constructing a new Digest::MD5 object with each loop. eg.

    my $md5 = Digest::MD5->new; while (<CONFIGFILE>) { chomp; $md5->reset; { local *FILE; open (FILE, $_) || warn $!; binmode FILE; $md5->addfile(*FILE); close FILE; } # stuff with $md5 digest methods }

    Now, its beyond this segment of code that the purpose of some of the code flow became a little hazy to me - The second while loop could me written differently with a direct comparison between scalar contexts of the arrays without the use of the superfluous counter variables - Also too, the use of $a and $b as variable names is not a good choice given the magic role which these play within Perl. eg. sort.

    Anyhow, while I would write the code differently, if it does what you need, good luck and well done on your efforts thus far.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'