<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Digest::HMAC;
$VERSION = "1.03";

use strict;

# OO interface

sub new
{
    my($class, $key, $hasher, $block_size) =  @_;
    $block_size ||= 64;
    $key = $hasher-&gt;new-&gt;add($key)-&gt;digest if length($key) &gt; $block_size;

    my $self = bless {}, $class;
    $self-&gt;{k_ipad} = $key ^ (chr(0x36) x $block_size);
    $self-&gt;{k_opad} = $key ^ (chr(0x5c) x $block_size);
    $self-&gt;{hasher} = $hasher-&gt;new-&gt;add($self-&gt;{k_ipad});
    $self;
}

sub reset
{
    my $self = shift;
    $self-&gt;{hasher}-&gt;reset-&gt;add($self-&gt;{k_ipad});
    $self;
}

sub add     { my $self = shift; $self-&gt;{hasher}-&gt;add(@_);     $self; }
sub addfile { my $self = shift; $self-&gt;{hasher}-&gt;addfile(@_); $self; }

sub _digest
{
    my $self = shift;
    my $inner_digest = $self-&gt;{hasher}-&gt;digest;
    $self-&gt;{hasher}-&gt;reset-&gt;add($self-&gt;{k_opad}, $inner_digest);
}

sub digest    { shift-&gt;_digest-&gt;digest;    }
sub hexdigest { shift-&gt;_digest-&gt;hexdigest; }
sub b64digest { shift-&gt;_digest-&gt;b64digest; }


# Functional interface

require Exporter;
*import = \&amp;Exporter::import;
use vars qw(@EXPORT_OK);
@EXPORT_OK = qw(hmac hmac_hex);

sub hmac
{
    my($data, $key, $hash_func, $block_size) = @_;
    $block_size ||= 64;
    $key = &amp;$hash_func($key) if length($key) &gt; $block_size;

    my $k_ipad = $key ^ (chr(0x36) x $block_size);
    my $k_opad = $key ^ (chr(0x5c) x $block_size);

    &amp;$hash_func($k_opad, &amp;$hash_func($k_ipad, $data));
}

sub hmac_hex { unpack("H*", &amp;hmac); }

1;

__END__

=head1 NAME

Digest::HMAC - Keyed-Hashing for Message Authentication

=head1 SYNOPSIS

 # Functional style
 use Digest::HMAC qw(hmac hmac_hex);
 $digest = hmac($data, $key, \&amp;myhash);
 print hmac_hex($data, $key, \&amp;myhash);

 # OO style
 use Digest::HMAC;
 $hmac = Digest::HMAC-&gt;new($key, "Digest::MyHash");

 $hmac-&gt;add($data);
 $hmac-&gt;addfile(*FILE);

 $digest = $hmac-&gt;digest;
 $digest = $hmac-&gt;hexdigest;
 $digest = $hmac-&gt;b64digest;

=head1 DESCRIPTION

HMAC is used for message integrity checks between two parties that
share a secret key, and works in combination with some other Digest
algorithm, usually MD5 or SHA-1.  The HMAC mechanism is described in
RFC 2104.

HMAC follow the common C&lt;Digest::&gt; interface, but the constructor
takes the secret key and the name of some other simple C&lt;Digest::&gt;
as argument.

The hmac() and hmac_hex() functions and the Digest::HMAC-&gt;new() constructor
takes an optional $blocksize argument as well.  The HMAC algorithm assumes the
digester to hash by iterating a basic compression function on blocks of data
and the $blocksize should match the byte-length of such blocks.

The default $blocksize is 64 which is suitable for the MD5 and SHA-1 digest
functions.  For stronger algorithms the blocksize probably needs to be
increased.

=head1 SEE ALSO

L&lt;Digest::HMAC_MD5&gt;, L&lt;Digest::HMAC_SHA1&gt;

RFC 2104

=head1 AUTHORS

Graham Barr &lt;gbarr@ti.com&gt;, Gisle Aas &lt;gisle@aas.no&gt;

=cut
</pre></body></html>