File Coverage

blib/lib/Digest/PSHA.pm
Criterion Covered Total %
statement 39 41 95.1
branch 6 10 60.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 2 0.0
total 55 65 84.6


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   34688 use 5.0008;
  2         6  
  2         64  
6 2     2   8 use strict;
  2         4  
  2         64  
7 2     2   16 use warnings;
  2         9  
  2         51  
8 2     2   8 use Carp;
  2         2  
  2         146  
9 2     2   2287 use Digest::SHA;
  2         7518  
  2         149  
10 2     2   1394 use Digest::HMAC qw / hmac /;
  2         1471  
  2         912  
11              
12             require Exporter;
13              
14             our @ISA = qw(Exporter);
15             our @EXPORT_OK = qw / p_sha1 p_sha256 /;
16             our $VERSION = '0.51';
17              
18             sub p_sha1
19             {
20 3     3 0 902 my ($secret, $salt, $bytes, $offset) = @_;
21 3 50       14 $bytes = 128 unless defined $bytes;
22 3         47 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 3     3 0 513 my ($secret, $salt, $bytes, $offset) = @_;
29 3 50       9 $bytes = 256 unless defined $bytes;
30 3         40 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 6     6   14 my ($secret, $salt, $bytes, $offset, $hash_func, $func_hash_len ) = @_;
37 6         7 my ($buf1, $buf2, $t);
38 6 50       16 $offset = 0 unless defined $offset;
39              
40 6 50 33     56 if ($offset % 8 || $bytes % 8 ) {
41 0         0 carp "Wrong parameters length [$bytes] and/or offset [$offset], they are in bits!";
42 0         0 return undef;
43             }
44              
45 6         18 my $bytes_tot = ($offset + $bytes) / 8 ;
46 6         7 $buf1 = $salt;
47 6         8 my $buf = q{};
48 6         647 for ( my $i=0; $i < $bytes_tot ;) {
49 12         37 $buf2 = $buf1 = hmac($buf1, $secret, $hash_func);
50 12         269 substr($buf2, $func_hash_len) = $salt;
51 12         28 $t = hmac($buf2, $secret, $hash_func);
52 12         250 for (my $x = 0; $x < length($t); $x++) {
53 291 100       619 last if ( $i >= $bytes_tot );
54 288         708 substr($buf, $i++, 1, substr($t, $x, 1) );
55             }
56             }
57 6         85 return $buf;
58             }
59              
60             1;
61              
62             __END__