File Coverage

blib/lib/Digest/PSHA.pm
Criterion Covered Total %
statement 18 41 43.9
branch 0 10 0.0
condition 0 3 0.0
subroutine 6 9 66.6
pod 0 2 0.0
total 24 65 36.9


line stmt bran cond sub pod time code
1             package Digest::PSHA;
2              
3             # $Id: PSHA.pm,v 1.2 2014-10-23 12:21:56 swaj Exp $
4              
5 2     2   39784 use 5.0008;
  2         8  
  2         84  
6 2     2   11 use strict;
  2         4  
  2         92  
7 2     2   14 use warnings;
  2         11  
  2         71  
8 2     2   11 use Carp;
  2         3  
  2         221  
9 2     2   1412 use Digest::SHA;
  2         6882  
  2         130  
10 2     2   1439 use Digest::HMAC qw / hmac /;
  2         1168  
  2         1027  
11              
12             require Exporter;
13              
14             our @ISA = qw(Exporter);
15             our @EXPORT_OK = qw / p_sha1 p_sha256 /;
16             our $VERSION = '0.50';
17              
18             sub p_sha1
19             {
20 0     0 0   my ($secret, $salt, $bytes, $offset) = @_;
21 0 0         $bytes = 128 unless defined $bytes;
22 0           substr ( _digest ($secret, $salt, $bytes, $offset, \&Digest::SHA::sha1, length(Digest::SHA::sha1(q{}) ) ),
23             $offset/8, $bytes/8);
24             }
25              
26             sub p_sha256
27             {
28 0     0 0   my ($secret, $salt, $bytes, $offset) = @_;
29 0 0         $bytes = 256 unless defined $bytes;
30 0           substr ( _digest ($secret, $salt, $bytes, $offset, \&Digest::SHA::sha256, length(Digest::SHA::sha256(q{}) ) ),
31             $offset/8, $bytes/8);
32             }
33              
34             sub _digest
35             {
36 0     0     my ($secret, $salt, $bytes, $offset, $hash_func, $func_hash_len ) = @_;
37 0           my ($buf1, $buf2, $t);
38 0 0         $offset = 0 unless defined $offset;
39              
40 0 0 0       if ($offset % 8 || $bytes % 8 ) {
41 0           carp "Wrong parameters length [$bytes] and/or offset [$offset], they are in bits!";
42 0           return undef;
43             }
44              
45 0           my $bytes_tot = ($offset + $bytes) / 8 ;
46 0           $buf1 = $salt;
47 0           my $buf = q{};
48 0           for ( my $i=0; $i < $bytes_tot ;) {
49 0           $buf2 = $buf1 = hmac($buf1, $secret, $hash_func);
50 0           substr($buf2, $func_hash_len) = $salt;
51 0           $t = hmac($buf2, $secret, $hash_func);
52 0           for (my $x = 0; $x < length($t); $x++) {
53 0 0         last if ( $i >= $bytes_tot );
54 0           substr($buf, $i++, 1, substr($t, $x, 1) );
55             }
56             }
57 0           return $buf;
58             }
59              
60             1;
61              
62             __END__