Monday, August 6, 2012

Calculating md5sum of a given file

use Digest::MD5; # make sure to have this package added in your perl script

# Function Name : md5sum
# Input            : File for which md5sum is requested
# Output        : md5sum of the file
# Description    : This function will calculate the md5sum (checksum) of the file and return it.
# Created By    : Rinkesh Bansal
#
sub md5sum($)
{
    my $file = shift;
    die "Error::File ($file) does not exist!!!"    if (not -e $file);
    open (FO, $file);
    binmode(FO);
    my $md5 = Digest::MD5->new;
    while ()
    {
        $md5->add($_);
    }
    close(FO);
    my $checksum = $md5->hexdigest;
#    print "$checksum $file\n";
    return $checksum;
}

No comments:

Post a Comment