File Coverage

lib/Digest/PBKDF2.pm
Criterion Covered Total %
statement 40 40 100.0
branch 3 4 75.0
condition 4 6 66.6
subroutine 10 10 100.0
pod 5 5 100.0
total 62 65 95.3


line stmt bran cond sub pod time code
1             package Digest::PBKDF2;
2              
3 1     1   902 use strict;
  1         2  
  1         39  
4 1     1   6 use warnings;
  1         3  
  1         36  
5 1     1   811 use parent "Digest::base";
  1         317  
  1         6  
6 1     1   931 use Crypt::PBKDF2 0.112020;
  1         611456  
  1         92  
7              
8             BEGIN {
9 1     1   443 our $VERSION = '0.010'; # VERSION
10             }
11              
12             #ABSTRACT: This module is a subclass of Digest using the Crypt::PBKDF2 algorithm.
13              
14             sub new {
15 2     2 1 179532 my ( $class, %params ) = @_;
16 2   50     24 my $encoding = $params{encoding} || 'crypt';
17 2         27 return bless { _entries => [], _data => undef, encoding => $encoding }, $class;
18             }
19              
20             sub clone {
21 2     2 1 864 my $self = shift;
22 2         11 my $clone = {
23             _data => $self->{_data},
24             _entries => $self->{_entries},
25             encoding => $self->{encoding},
26             };
27 2         13 return bless $clone, ref $self;
28             }
29              
30             sub add {
31 4     4 1 4848 my $self = shift;
32 4 50       14 if (@_) {
33 4         5 push @{ $self->{_entries} }, join '', @_;
  4         25  
34 4         11 $self->{_data} .= join '', @_;
35             }
36 4         17 $self;
37             }
38              
39             sub reset {
40 4     4 1 12 my $self = shift;
41 4         20 delete $self->{_data};
42 4         13 delete $self->{_entries};
43 4         11 delete $self->{encoding};
44 4         8 $self;
45             }
46              
47             sub digest {
48 4     4 1 1426 my $self = shift;
49 4         32 my @string = split '', $self->{_data};
50              
51 4         9 my $salt;
52              
53 4         30 $salt = join( '', splice( @string, 0, length( $self->{_entries}->[0] ) ) )
54 4 100       8 if @{ $self->{_entries} } > 1;
55 4         14 my $data = join( '', @string );
56              
57 4   50     184 my $crypt = Crypt::PBKDF2->new( encoding => ($self->{encoding}||'ldap'), salt_len => length($salt||'') );
      100        
58 4         3299 my $return = $crypt->generate( $data, $salt );
59 4         43259 $self->reset;
60 4         155 $return;
61             }
62              
63             1;
64              
65             __END__
66              
67             =head1 NAME
68              
69             Digest::PBKDF2
70             A minimalist Digest module using the PBKDF2 algorithm.
71              
72             =head1 NOTICE
73              
74             You can only use one salt, a pre-salt, with this module. It is not smart enough
75             to do post-salts.
76              
77             =head1 SYNOPSIS
78              
79             my $digest = Digest::PBKDF2->new; # Or...
80             my $digest = Digest::PBKDF2->new(encoding => 'ldap');
81             $digest->add('mysalt'); # salt = 'mysalt'
82             $digest->add('k3wLP@$$w0rd'); # password = 'k3wLP@$$w0rd'
83              
84             $digest->add('eX+ens10n'); # password = 'k3wLP@$$w0rdeX+ens10n'
85              
86             my $result = $digest->digest; # $PBKDF2$HMACSHA1:1000:bXlzYWx0$4P9pwp
87             # LoF+eq5jwUbMw05qRQyZs=
88              
89             That's about it.
90              
91             =head1 METHODS
92              
93             =over
94              
95             =item new
96              
97             Create a new Digest::PBKDF2 object. This defaults to using the "ldap" encoding
98             available in Crypt::PBKDF2--please see L<Crypt::PBKDF2> for details.
99              
100             =item clone
101              
102             Copies the data and state from the original Digest::PBKDF2 object,
103             and returns a new object.
104              
105             =item add
106              
107             Pass this method your salt and data chunks. They are stored up
108             until you call digest.
109              
110             =item digest
111              
112             This encrypts your data and returns the encrypted string.
113              
114             =item reset
115              
116             After calling digest, the module calls reset on its self,
117             clearing data and the record of how many additions were made to the data
118             to be digested.
119              
120             =back
121              
122             =head1 SEE ALSO
123              
124             L<Crypt::PBKDF2>
125             L<Digest>
126              
127             =head1 AUTHOR
128              
129             Amiri Barksdale, E<lt>abarksdale@campusexplorer.comE<gt>
130              
131             =head1 COPYRIGHT
132              
133             Copyright (c) 2014 by Campus Explorer, Inc.
134              
135             L<http://www.campusexplorer.com>
136              
137             =head1 LICENSE
138              
139             This library is free software; you can redistribute it and/or modify it
140             under the same terms as Perl itself.