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