PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. package Digest::HMAC;
  2. $VERSION = "1.00";
  3. use strict;
  4. # OO interface
  5. sub new
  6. {
  7. my($class, $key, $hasher, $block_size) = @_;
  8. $block_size ||= 64;
  9. $key = $hasher->new->add($key)->digest if length($key) > $block_size;
  10. my $self = bless {}, $class;
  11. $self->{k_ipad} = $key ^ (chr(0x36) x $block_size);
  12. $self->{k_opad} = $key ^ (chr(0x5c) x $block_size);
  13. $self->{hasher} = $hasher->new->add($self->{k_ipad});
  14. $self;
  15. }
  16. sub reset
  17. {
  18. my $self = shift;
  19. $self->{hasher}->reset->add($self->{k_ipad});
  20. $self;
  21. }
  22. sub add { my $self = shift; $self->{hasher}->add(@_); $self; }
  23. sub addfile { my $self = shift; $self->{hasher}->addfile(@_); $self; }
  24. sub _digest
  25. {
  26. my $self = shift;
  27. my $inner_digest = $self->{hasher}->digest;
  28. $self->{hasher}->reset->add($self->{k_opad}, $inner_digest);
  29. }
  30. sub digest { shift->_digest->digest; }
  31. sub hexdigest { shift->_digest->hexdigest; }
  32. sub b64digest { shift->_digest->b64digest; }
  33. # Functional interface
  34. require Exporter;
  35. *import = \&Exporter::import;
  36. use vars qw(@EXPORT_OK);
  37. @EXPORT_OK = qw(hmac hmac_hex);
  38. sub hmac
  39. {
  40. my($data, $key, $hash_func, $block_size) = @_;
  41. $block_size ||= 64;
  42. $key = &$hash_func($key) if length($key) > $block_size;
  43. my $k_ipad = $key ^ (chr(0x36) x $block_size);
  44. my $k_opad = $key ^ (chr(0x5c) x $block_size);
  45. &$hash_func($k_opad, &$hash_func($k_ipad, $data));
  46. }
  47. sub hmac_hex { unpack("H*", &hmac); }
  48. 1;
  49. __END__
  50. =head1 NAME
  51. Digest::HMAC - Keyed-Hashing for Message Authentication
  52. =head1 SYNOPSIS
  53. # Functional style
  54. use Digest::HMAC qw(hmac hmac_hex);
  55. $digest = hmac($data, $key, \&myhash);
  56. print hmac_hex($data, $key, \&myhash);
  57. # OO style
  58. use Digest::HMAC;
  59. $hmac = Digest::HMAC->new($key, "Digest::MyHash");
  60. $hmac->add($data);
  61. $hmac->addfile(*FILE);
  62. $digest = $hmac->digest;
  63. $digest = $hmac->hexdigest;
  64. $digest = $hmac->b64digest;
  65. =head1 DESCRIPTION
  66. HMAC is used for message integrity checks between two parties that
  67. share a secret key, and works in combination with some other Digest
  68. algorithm, usually MD5 or SHA-1. The HMAC mechanism is described in
  69. RFC 2104.
  70. HMAC follow the common C<Digest::> interface, but the constructor
  71. takes the secret key and the name of some other simple C<Digest::>
  72. as argument.
  73. =head1 SEE ALSO
  74. L<Digest::HMAC_MD5>, L<Digest::HMAC_SHA1>
  75. RFC 2104
  76. =head1 AUTHORS
  77. Graham Barr <gbarr@ti.com>, Gisle Aas <gisle@aas.no>
  78. =cut