| 1 | package Crypt::PBKDF2::Hash::HMACSHA3; | ||
| 2 | # ABSTRACT: HMAC-SHA3 support for Crypt::PBKDF2 using Digest::SHA | ||
| 3 | our $VERSION = '0.261630'; # VERSION | ||
| 4 | our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY | ||
| 5 | use Moo 2; | ||
| 6 | use strictures 2; | ||
| 7 | use namespace::autoclean; | ||
| 8 | use Digest::HMAC 1.01 (); | ||
| 9 | use Digest::SHA3 0.22 (); | ||
| 10 | use Type::Tiny; | ||
| 11 | use Types::Standard qw(Enum); | ||
| 12 | |||
| 13 | with 'Crypt::PBKDF2::Hash'; | ||
| 14 | |||
| 15 | has 'sha_size' => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => Type::Tiny->new( | ||
| 18 | name => 'SHASize', | ||
| 19 | parent => Enum[qw( 224 256 384 512 )], | ||
| 20 | display_name => 'valid number of bits for SHA-2', | ||
| 21 | ), | ||
| 22 | default => 256, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has '_hasher' => ( | ||
| 26 | is => 'lazy', | ||
| 27 | init_arg => undef, | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub _build__hasher { | ||
| 31 | my $self = shift; | ||
| 32 | my $shasize = $self->sha_size; | ||
| 33 | |||
| 34 | return Digest::SHA3->can("sha3_$shasize"); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub hash_len { | ||
| 38 | my $self = shift; | ||
| 39 | return $self->sha_size() / 8; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub generate { | ||
| 43 | my ($self, $data, $key) = @_; | ||
| 44 | return Digest::HMAC::hmac($data, $key, $self->_hasher); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub to_algo_string { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | return $self->sha_size; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub from_algo_string { | ||
| 54 | my ($class, $str) = @_; | ||
| 55 | |||
| 56 | return $class->new( sha_size => $str ); | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ |