home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2004 July / APC0407D2.iso / workshop / apache / files / ActivePerl-5.6.1.638-MSWin32-x86.msi / _ed3bd99aa792ccfc02d5d7100043fa3c < prev    next >
Encoding:
Text File  |  2004-04-13  |  2.5 KB  |  112 lines

  1. package Digest::HMAC;
  2. $VERSION = "1.01";
  3.  
  4. use strict;
  5.  
  6. # OO interface
  7.  
  8. sub 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.  
  21. sub reset
  22. {
  23.     my $self = shift;
  24.     $self->{hasher}->reset->add($self->{k_ipad});
  25.     $self;
  26. }
  27.  
  28. sub add     { my $self = shift; $self->{hasher}->add(@_);     $self; }
  29. sub addfile { my $self = shift; $self->{hasher}->addfile(@_); $self; }
  30.  
  31. sub _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.  
  38. sub digest    { shift->_digest->digest;    }
  39. sub hexdigest { shift->_digest->hexdigest; }
  40. sub b64digest { shift->_digest->b64digest; }
  41.  
  42.  
  43. # Functional interface
  44.  
  45. require Exporter;
  46. *import = \&Exporter::import;
  47. use vars qw(@EXPORT_OK);
  48. @EXPORT_OK = qw(hmac hmac_hex);
  49.  
  50. sub 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.  
  62. sub hmac_hex { unpack("H*", &hmac); }
  63.  
  64. 1;
  65.  
  66. __END__
  67.  
  68. =head1 NAME
  69.  
  70. Digest::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.  
  92. HMAC is used for message integrity checks between two parties that
  93. share a secret key, and works in combination with some other Digest
  94. algorithm, usually MD5 or SHA-1.  The HMAC mechanism is described in
  95. RFC 2104.
  96.  
  97. HMAC follow the common C<Digest::> interface, but the constructor
  98. takes the secret key and the name of some other simple C<Digest::>
  99. as argument.
  100.  
  101. =head1 SEE ALSO
  102.  
  103. L<Digest::HMAC_MD5>, L<Digest::HMAC_SHA1>
  104.  
  105. RFC 2104
  106.  
  107. =head1 AUTHORS
  108.  
  109. Graham Barr <gbarr@ti.com>, Gisle Aas <gisle@aas.no>
  110.  
  111. =cut
  112.