Perl MD5sum Using Digest::MD5

There is a binary utility on most flavors of linux that allows you to compare if a file is exactly the same as another. Here is how you get MD5 info using Perl:

I wrote something similar a few years ago and couldn’t find it. So I googled the docs on Digest::MD5 and found this wonderful example.
You would want to use an MD5 sum for comparing a file after you’ve transmitted it, to ensure it is the same file without errors.
Perl MD5Sum Using Digest::MD5

# credit:
use warnings;
use strict;
use Digest::MD5;
sub md5sum{
  my $file = shift;
  my $digest = "";
    open(FILE, $file) or die "Can't find file $file\n";
    my $ctx = Digest::MD5->new;
    $digest = $ctx->hexdigest;
    print $@;
    return "";
  return $digest;
sub usage{
  print "usage: ./ filename\n";
  exit 1;
if($#ARGV + 1 != 1){
my $fname = $ARGV[0];
my $md5 =  md5sum($fname);
if($md5 ne ""){
  print $md5." ".$fname."\n";
  exit 1;
exit 0; 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.