| 1 | package Digest;
|
|---|
| 2 |
|
|---|
| 3 | use strict;
|
|---|
| 4 | use vars qw($VERSION %MMAP $AUTOLOAD);
|
|---|
| 5 |
|
|---|
| 6 | $VERSION = "1.14";
|
|---|
| 7 |
|
|---|
| 8 | %MMAP = (
|
|---|
| 9 | "SHA-1" => ["Digest::SHA1", ["Digest::SHA", 1], ["Digest::SHA2", 1]],
|
|---|
| 10 | "SHA-224" => [["Digest::SHA", 224]],
|
|---|
| 11 | "SHA-256" => [["Digest::SHA", 256], ["Digest::SHA2", 256]],
|
|---|
| 12 | "SHA-384" => [["Digest::SHA", 384], ["Digest::SHA2", 384]],
|
|---|
| 13 | "SHA-512" => [["Digest::SHA", 512], ["Digest::SHA2", 512]],
|
|---|
| 14 | "HMAC-MD5" => "Digest::HMAC_MD5",
|
|---|
| 15 | "HMAC-SHA-1" => "Digest::HMAC_SHA1",
|
|---|
| 16 | "CRC-16" => [["Digest::CRC", type => "crc16"]],
|
|---|
| 17 | "CRC-32" => [["Digest::CRC", type => "crc32"]],
|
|---|
| 18 | "CRC-CCITT" => [["Digest::CRC", type => "crcccitt"]],
|
|---|
| 19 | );
|
|---|
| 20 |
|
|---|
| 21 | sub new
|
|---|
| 22 | {
|
|---|
| 23 | shift; # class ignored
|
|---|
| 24 | my $algorithm = shift;
|
|---|
| 25 | my $impl = $MMAP{$algorithm} || do {
|
|---|
| 26 | $algorithm =~ s/\W+//;
|
|---|
| 27 | "Digest::$algorithm";
|
|---|
| 28 | };
|
|---|
| 29 | $impl = [$impl] unless ref($impl);
|
|---|
| 30 | my $err;
|
|---|
| 31 | for (@$impl) {
|
|---|
| 32 | my $class = $_;
|
|---|
| 33 | my @args;
|
|---|
| 34 | ($class, @args) = @$class if ref($class);
|
|---|
| 35 | no strict 'refs';
|
|---|
| 36 | unless (exists ${"$class\::"}{"VERSION"}) {
|
|---|
| 37 | eval "require $class";
|
|---|
| 38 | if ($@) {
|
|---|
| 39 | $err ||= $@;
|
|---|
| 40 | next;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | return $class->new(@args, @_);
|
|---|
| 44 | }
|
|---|
| 45 | die $err;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | sub AUTOLOAD
|
|---|
| 49 | {
|
|---|
| 50 | my $class = shift;
|
|---|
| 51 | my $algorithm = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
|
|---|
| 52 | $class->new($algorithm, @_);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | 1;
|
|---|
| 56 |
|
|---|
|
|---|