Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

md5sum

check md5sums

#!/usr/bin/perl
# credit: http://coding.debuntu.org/perl-calculate-md5-sum-file
use warnings;
use strict;
use Digest::MD5;
     
sub md5sum{
  my $file = shift;
  my $digest = "";
  eval{
    open(FILE, $file) or die "Can't find file $file\n";
    my $ctx = Digest::MD5->new;
    $ctx->addfile(*FILE);
    $digest = $ctx->hexdigest;
    close(FILE);
  };
  if($@){
    print $@;
    return "";
  }
  return $digest;
}
sub usage{
  print "usage: ./md5sum.pl filename\n";
  exit 1;
}
if($#ARGV + 1 != 1){
  usage();
}
my $fname = $ARGV[0];
my $md5 =  md5sum($fname);
if($md5 ne ""){
  print $md5." ".$fname."\n";
}else{
  exit 1;
}
 
exit 0;