"Fossies" - the Fresh Open Source Software Archive

Member "Digest-HMAC-1.04/lib/Digest/HMAC.pm" (1 Apr 2021, 3013 Bytes) of package /linux/privat/Digest-HMAC-1.04.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package Digest::HMAC;
    2 our $VERSION = '1.04'; # VERSION
    3 our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
    4 
    5 use strict;
    6 
    7 # OO interface
    8 
    9 sub new
   10 {
   11     my($class, $key, $hasher, $block_size) =  @_;
   12     $block_size ||= 64;
   13     $key = $hasher->new->add($key)->digest if length($key) > $block_size;
   14 
   15     my $self = bless {}, $class;
   16     $self->{k_ipad} = $key ^ (chr(0x36) x $block_size);
   17     $self->{k_opad} = $key ^ (chr(0x5c) x $block_size);
   18     $self->{hasher} = $hasher->new->add($self->{k_ipad});
   19     $self;
   20 }
   21 
   22 sub reset
   23 {
   24     my $self = shift;
   25     $self->{hasher}->reset->add($self->{k_ipad});
   26     $self;
   27 }
   28 
   29 sub add     { my $self = shift; $self->{hasher}->add(@_);     $self; }
   30 sub addfile { my $self = shift; $self->{hasher}->addfile(@_); $self; }
   31 
   32 sub _digest
   33 {
   34     my $self = shift;
   35     my $inner_digest = $self->{hasher}->digest;
   36     $self->{hasher}->reset->add($self->{k_opad}, $inner_digest);
   37 }
   38 
   39 sub digest    { shift->_digest->digest;    }
   40 sub hexdigest { shift->_digest->hexdigest; }
   41 sub b64digest { shift->_digest->b64digest; }
   42 
   43 
   44 # Functional interface
   45 
   46 require Exporter;
   47 *import = \&Exporter::import;
   48 use vars qw(@EXPORT_OK);
   49 @EXPORT_OK = qw(hmac hmac_hex);
   50 
   51 sub hmac
   52 {
   53     my($data, $key, $hash_func, $block_size) = @_;
   54     $block_size ||= 64;
   55     $key = &$hash_func($key) if length($key) > $block_size;
   56 
   57     my $k_ipad = $key ^ (chr(0x36) x $block_size);
   58     my $k_opad = $key ^ (chr(0x5c) x $block_size);
   59 
   60     &$hash_func($k_opad, &$hash_func($k_ipad, $data));
   61 }
   62 
   63 sub hmac_hex { unpack("H*", &hmac); }
   64 
   65 1;
   66 
   67 __END__
   68 
   69 =head1 NAME
   70 
   71 Digest::HMAC - Keyed-Hashing for Message Authentication
   72 
   73 =head1 SYNOPSIS
   74 
   75  # Functional style
   76  use Digest::HMAC qw(hmac hmac_hex);
   77  $digest = hmac($data, $key, \&myhash);
   78  print hmac_hex($data, $key, \&myhash);
   79 
   80  # OO style
   81  use Digest::HMAC;
   82  $hmac = Digest::HMAC->new($key, "Digest::MyHash");
   83 
   84  $hmac->add($data);
   85  $hmac->addfile(*FILE);
   86 
   87  $digest = $hmac->digest;
   88  $digest = $hmac->hexdigest;
   89  $digest = $hmac->b64digest;
   90 
   91 =head1 DESCRIPTION
   92 
   93 HMAC is used for message integrity checks between two parties that
   94 share a secret key, and works in combination with some other Digest
   95 algorithm, usually MD5 or SHA-1.  The HMAC mechanism is described in
   96 RFC 2104.
   97 
   98 HMAC follow the common C<Digest::> interface, but the constructor
   99 takes the secret key and the name of some other simple C<Digest::>
  100 as argument.
  101 
  102 The hmac() and hmac_hex() functions and the Digest::HMAC->new() constructor
  103 takes an optional $blocksize argument as well.  The HMAC algorithm assumes the
  104 digester to hash by iterating a basic compression function on blocks of data
  105 and the $blocksize should match the byte-length of such blocks.
  106 
  107 The default $blocksize is 64 which is suitable for the MD5 and SHA-1 digest
  108 functions.  For stronger algorithms the blocksize probably needs to be
  109 increased.
  110 
  111 =head1 SEE ALSO
  112 
  113 L<Digest::HMAC_MD5>, L<Digest::HMAC_SHA1>
  114 
  115 RFC 2104
  116 
  117 =head1 MAINTAINER
  118 
  119 Andrew Rodland <arodland@cpan.org>
  120 
  121 =head1 ORIGINAL AUTHORS
  122 
  123 Graham Barr <gbarr@ti.com>, Gisle Aas <gisle@aas.no>
  124 
  125 =cut