1package Digest::file; 2 3use strict; 4use warnings; 5 6use Exporter (); 7use Carp qw(croak); 8use Digest (); 9 10our $VERSION = "1.20"; 11our @ISA = qw(Exporter); 12our @EXPORT_OK = qw(digest_file_ctx digest_file digest_file_hex digest_file_base64); 13 14sub digest_file_ctx { 15 my $file = shift; 16 croak("No digest algorithm specified") unless @_; 17 open( my $fh, "<", $file ) || croak("Can't open '$file': $!"); 18 binmode($fh); 19 my $ctx = Digest->new(@_); 20 $ctx->addfile($fh); 21 close($fh); 22 return $ctx; 23} 24 25sub digest_file { 26 digest_file_ctx(@_)->digest; 27} 28 29sub digest_file_hex { 30 digest_file_ctx(@_)->hexdigest; 31} 32 33sub digest_file_base64 { 34 digest_file_ctx(@_)->b64digest; 35} 36 371; 38 39__END__ 40 41=head1 NAME 42 43Digest::file - Calculate digests of files 44 45=head1 SYNOPSIS 46 47 # Poor mans "md5sum" command 48 use Digest::file qw(digest_file_hex); 49 for (@ARGV) { 50 print digest_file_hex($_, "MD5"), " $_\n"; 51 } 52 53=head1 DESCRIPTION 54 55This module provide 3 convenience functions to calculate the digest 56of files. The following functions are provided: 57 58=over 59 60=item digest_file( $file, $algorithm, [$arg,...] ) 61 62This function will calculate and return the binary digest of the bytes 63of the given file. The function will croak if it fails to open or 64read the file. 65 66The $algorithm is a string like "MD2", "MD5", "SHA-1", "SHA-512". 67Additional arguments are passed to the constructor for the 68implementation of the given algorithm. 69 70=item digest_file_hex( $file, $algorithm, [$arg,...] ) 71 72Same as digest_file(), but return the digest in hex form. 73 74=item digest_file_base64( $file, $algorithm, [$arg,...] ) 75 76Same as digest_file(), but return the digest as a base64 encoded 77string. 78 79=back 80 81=head1 SEE ALSO 82 83L<Digest> 84