CC: | Ivan Shmakov <oneingray [...] gmail.com> |
Subject: | Digest::HMAC->new () is "incompatible" with Digest::SHA (512), etc. |
Date: | Sat, 06 Apr 2013 11:57:37 +0000 |
To: | bug-Digest-HMAC [...] rt.cpan.org |
From: | Ivan Shmakov <oneingray [...] gmail.com> |
[Forwarding Debian Bug#700617.]
It isn't currently possible to use the Digest::HMAC module's OO
interface along with the Digest subclasses whose constructors
require an argument, such as the Digest::SHA class, or
Digest->new () itself.
The patch MIME'd provides a way for the caller to pass a
prepared Digest instance, which is then clone ()'d and reset ()
to produce a “clean” Digest object.
Examples:
require Digest::HMAC;
require Digest::SHA;
my $hmac
= Digest::HMAC->new ($key, Digest::SHA->new (256));
## check, e. g., [1]
my $hmac
= Digest::HMAC->new ($key, Digest::SHA->new (384), 128);
my $hmac
= Digest::HMAC->new ($key, Digest::SHA->new (512), 128);
[1] https://bugzilla.mozilla.org/show_bug.cgi?id=313196
--
FSF associate member #7257 http://hfday.org/
--- HMAC.pm 2011-07-25 16:51:15.000000000 +0000
+++ HMAC.pm 2013-02-15 08:55:09.000000000 +0000
@@ -9,12 +9,16 @@
{
my($class, $key, $hasher, $block_size) = @_;
$block_size ||= 64;
- $key = $hasher->new->add($key)->digest if length($key) > $block_size;
+ my $d
+ = (ref($hasher) eq ""
+ ? $hasher->new()
+ : $hasher->clone()->reset());
+ $key = $d->add($key)->digest if length($key) > $block_size;
my $self = bless {}, $class;
$self->{k_ipad} = $key ^ (chr(0x36) x $block_size);
$self->{k_opad} = $key ^ (chr(0x5c) x $block_size);
- $self->{hasher} = $hasher->new->add($self->{k_ipad});
+ $self->{hasher} = $d->add($self->{k_ipad});
$self;
}
@@ -79,6 +83,7 @@
# OO style
use Digest::HMAC;
$hmac = Digest::HMAC->new($key, "Digest::MyHash");
+ $hmac = Digest::HMAC->new($key, Digest::MyHash->new());
$hmac->add($data);
$hmac->addfile(*FILE);