PageRenderTime 50ms CodeModel.GetById 15ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/site/lib/digest/hmac.pm

#
Perl | 111 lines | 74 code | 33 blank | 4 comment | 4 complexity | 77e704acffbaadf6b182dc63642a6527 MD5 | raw file
  1package Digest::HMAC;
  2$VERSION = "1.00";
  3
  4use strict;
  5
  6# OO interface
  7
  8sub new
  9{
 10    my($class, $key, $hasher, $block_size) =  @_;
 11    $block_size ||= 64;
 12    $key = $hasher->new->add($key)->digest if length($key) > $block_size;
 13
 14    my $self = bless {}, $class;
 15    $self->{k_ipad} = $key ^ (chr(0x36) x $block_size);
 16    $self->{k_opad} = $key ^ (chr(0x5c) x $block_size);
 17    $self->{hasher} = $hasher->new->add($self->{k_ipad});
 18    $self;
 19}
 20
 21sub reset
 22{
 23    my $self = shift;
 24    $self->{hasher}->reset->add($self->{k_ipad});
 25    $self;
 26}
 27
 28sub add     { my $self = shift; $self->{hasher}->add(@_);     $self; }
 29sub addfile { my $self = shift; $self->{hasher}->addfile(@_); $self; }
 30
 31sub _digest
 32{
 33    my $self = shift;
 34    my $inner_digest = $self->{hasher}->digest;
 35    $self->{hasher}->reset->add($self->{k_opad}, $inner_digest);
 36}
 37
 38sub digest    { shift->_digest->digest;    }
 39sub hexdigest { shift->_digest->hexdigest; }
 40sub b64digest { shift->_digest->b64digest; }
 41
 42
 43# Functional interface
 44
 45require Exporter;
 46*import = \&Exporter::import;
 47use vars qw(@EXPORT_OK);
 48@EXPORT_OK = qw(hmac hmac_hex);
 49
 50sub hmac
 51{
 52    my($data, $key, $hash_func, $block_size) = @_;
 53    $block_size ||= 64;
 54    $key = &$hash_func($key) if length($key) > $block_size;
 55
 56    my $k_ipad = $key ^ (chr(0x36) x $block_size);
 57    my $k_opad = $key ^ (chr(0x5c) x $block_size);
 58
 59    &$hash_func($k_opad, &$hash_func($k_ipad, $data));
 60}
 61
 62sub hmac_hex { unpack("H*", &hmac); }
 63
 641;
 65
 66__END__
 67
 68=head1 NAME
 69
 70Digest::HMAC - Keyed-Hashing for Message Authentication
 71
 72=head1 SYNOPSIS
 73
 74 # Functional style
 75 use Digest::HMAC qw(hmac hmac_hex);
 76 $digest = hmac($data, $key, \&myhash);
 77 print hmac_hex($data, $key, \&myhash);
 78
 79 # OO style
 80 use Digest::HMAC;
 81 $hmac = Digest::HMAC->new($key, "Digest::MyHash");
 82
 83 $hmac->add($data);
 84 $hmac->addfile(*FILE);
 85
 86 $digest = $hmac->digest;
 87 $digest = $hmac->hexdigest;
 88 $digest = $hmac->b64digest;
 89
 90=head1 DESCRIPTION
 91
 92HMAC is used for message integrity checks between two parties that
 93share a secret key, and works in combination with some other Digest
 94algorithm, usually MD5 or SHA-1.  The HMAC mechanism is described in
 95RFC 2104.
 96
 97HMAC follow the common C<Digest::> interface, but the constructor
 98takes the secret key and the name of some other simple C<Digest::>
 99as argument.
100
101=head1 SEE ALSO
102
103L<Digest::HMAC_MD5>, L<Digest::HMAC_SHA1>
104
105RFC 2104
106
107=head1 AUTHORS
108
109Graham Barr <gbarr@ti.com>, Gisle Aas <gisle@aas.no>
110
111=cut