File Coverage

blib/lib/Blockchain/Ethereum/Keystore/KDF.pm
Criterion Covered Total %
statement 36 36 100.0
branch 2 2 100.0
condition n/a
subroutine 17 17 100.0
pod 0 10 0.0
total 55 65 84.6


line stmt bran cond sub pod time code
1             package Blockchain::Ethereum::Keystore::KDF;
2              
3 3     3   263756 use v5.26;
  3         25  
4 3     3   18 use strict;
  3         5  
  3         115  
5 3     3   17 use warnings;
  3         6  
  3         310  
6              
7             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
8             our $VERSION = '0.021'; # VERSION
9              
10 3     3   1605 use Crypt::KeyDerivation qw(pbkdf2);
  3         7507  
  3         245  
11 3     3   1577 use Crypt::ScryptKDF qw(scrypt_raw);
  3         7912  
  3         1970  
12              
13             sub new {
14 7     7 0 76 my ($class, %params) = @_;
15              
16 7         35 my $self = bless {}, $class;
17 7         28 for (qw(algorithm dklen n p r prf c salt)) {
18 56 100       206 $self->{$_} = $params{$_} if exists $params{$_};
19             }
20              
21 7         81 return $self;
22             }
23              
24             sub algorithm {
25 19     19 0 92 return shift->{algorithm};
26             }
27              
28             sub dklen {
29 19     19 0 2300836 return shift->{dklen};
30             }
31              
32             sub n {
33 16     16 0 64 return shift->{n};
34             }
35              
36             sub p {
37 16     16 0 71 return shift->{p};
38             }
39              
40             sub r {
41 16     16 0 66 return shift->{r};
42             }
43              
44             sub prf {
45 1     1 0 7 return shift->{prf};
46             }
47              
48             sub c {
49 3     3 0 14 return shift->{c};
50             }
51              
52             sub salt {
53 19     19 0 147 return shift->{salt};
54             }
55              
56             sub decode {
57 15     15 0 49 my ($self, $password) = @_;
58              
59 15         59 my $kdf_function = '_decode_kdf_' . $self->algorithm;
60 15         97 return $self->$kdf_function($password);
61             }
62              
63             sub _decode_kdf_pbkdf2 {
64 2     2   5 my ($self, $password) = @_;
65              
66 2         9 my $derived_key = pbkdf2($password, pack("H*", $self->salt), $self->c, 'SHA256', $self->dklen);
67              
68 2         35 return $derived_key;
69             }
70              
71             sub _decode_kdf_scrypt {
72 13     13   42 my ($self, $password) = @_;
73              
74 13         51 my $derived_key = scrypt_raw(
75             $password, #
76             pack("H*", $self->salt),
77             $self->n,
78             $self->r,
79             $self->p,
80             $self->dklen
81             );
82              
83 13         14632273 return $derived_key;
84             }
85              
86             1;
87              
88             __END__