OpenPGP/S2k.pm

filter: Statementstmt 70.2% 92 / 131 Branchbran 41.6% 10 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 69.2% 18 / 26 Podpod 60.0% 3 / 5 totaltotal 64.9% 124 / 191 CC 14 SCAR 13.3
CC
14
Cov
65%
init
12.0
init
12.0
set_hash
6.0
CRAP 3.8
« OpenPGP/Util.pm ↑ Summary OpenPGP/Config.pm »
1 package Crypt::OpenPGP::S2k;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 our $VERSION = '1.21'; # VERSION
6  
7 8 ▶use Crypt::OpenPGP::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Crypt::OpenPGP::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Crypt::OpenPGP::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use parent qw( Crypt::OpenPGP::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 our %TYPES = (
13 0 => 'Simple',
14 1 => 'Salted',
15 3 => 'Salt_Iter',
16 );
17  
18 9 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
19 9 my $class = shift;
20 9 my $type = shift;
21 9 ▶ $type = $TYPES{ $type } || $type;
Condition33%1/3
$Crypt::OpenPGP::S2k::TYPES{$type} || $type
l!l&&r!l&&!r
090
MC/DC0%0/2
$Crypt::OpenPGP::S2k::TYPES{$type} || $type
$Crypt::OpenPGP::S2k::TYPES{$type} $type
Truth table33%1/3
A || B
A= $Crypt::OpenPGP::S2k::TYPES{$type}B= $type
ABresult
1X1
011
000
22 9 ▶ return $class->error("Invalid type of S2k") unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 9 9
23 9 my $pkg = join '::', __PACKAGE__, $type;
24 9 my $s2k = bless { }, $pkg;
25 9 $s2k->init(@_);
26 }
27  
28 5 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
29 5 my $class = shift;
30 5 my($buf) = @_;
31 5 my $id = $buf->get_int8;
32 5 my $type = $TYPES{$id};
33 5 $class->new($type, $buf);
34 }
35  
36 0 ▶sub init { $_[0] }
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
37 7 ▶sub generate {
Subroutine generate: called CC=2 SCAR=6.9
Pod documented
38 7 my $s2k = shift;
39 7 my($passphrase, $keysize) = @_;
40 7 my($material, $pass) = ('', 0);
41 7 my $hash = $s2k->{hash};
42 7 ▶ while (length($material) < $keysize) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $material < $keysize) 14 7 21
43 14 my $pad = '' . chr(0) x $pass;
44 14 $material .= $s2k->s2k($passphrase, $pad);
45 14 $pass++;
46 }
47 7 substr($material, 0, $keysize);
48 }
49 0 ▶sub set_hash {
Subroutine set_hash: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my $s2k = shift;
51 0 my($hash_alg) = @_;
52 0 ▶ $s2k->{hash} = ref($hash_alg) ? $hash_alg :
Branch: 1 branch
BranchTrueFalseTotal
ref $hash_alg ? : 0 0 0
53 Crypt::OpenPGP::Digest->new($hash_alg);
54 }
55  
56 package Crypt::OpenPGP::S2k::Simple;
57 8 ▶use parent qw( Crypt::OpenPGP::S2k );
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 8 ▶use Crypt::OpenPGP::Constants qw( DEFAULT_DIGEST );
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
62 0 my $s2k = shift;
63 0 my($buf) = @_;
64 0 ▶ if ($buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf) { } 0 0 0
65 0 $s2k->{hash_alg} = $buf->get_int8;
66 }
67 else {
68 0 $s2k->{hash_alg} = DEFAULT_DIGEST;
69 }
70 0 ▶ if ($s2k->{hash_alg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2k->{"hash_alg"}) 0 0 0
71 0 $s2k->{hash} = Crypt::OpenPGP::Digest->new($s2k->{hash_alg});
72 }
73 0 $s2k;
74 }
75  
76 0 ▶sub s2k { $_[0]->{hash}->hash($_[2] . $_[1]) }
Subroutine s2k: not called CC=1 SCAR=6.9
77  
78 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
79 0 my $s2k = shift;
80 0 my $buf = Crypt::OpenPGP::Buffer->new;
81 0 $buf->put_int8(1);
82 0 $buf->put_int8($s2k->{hash_alg});
83 0 $buf->bytes;
84 }
85  
86 package Crypt::OpenPGP::S2k::Salted;
87 8 ▶use parent qw( Crypt::OpenPGP::S2k );
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 8 ▶use Crypt::OpenPGP::Constants qw( DEFAULT_DIGEST );
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
92 0 my $s2k = shift;
93 0 my($buf) = @_;
94 0 ▶ if ($buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf) { } 0 0 0
95 0 $s2k->{hash_alg} = $buf->get_int8;
96 0 $s2k->{salt} = $buf->get_bytes(8);
97 }
98 else {
99 0 $s2k->{hash_alg} = DEFAULT_DIGEST;
100 0 $s2k->{salt} = Crypt::OpenPGP::Util::get_random_bytes(8);
101 }
102 0 ▶ if ($s2k->{hash_alg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2k->{"hash_alg"}) 0 0 0
103 0 $s2k->{hash} = Crypt::OpenPGP::Digest->new($s2k->{hash_alg});
104 }
105 0 $s2k;
106 }
107  
108 0 ▶sub s2k { $_[0]->{hash}->hash($_[0]->{salt} . $_[2] . $_[1]) }
Subroutine s2k: not called CC=1 SCAR=6.9
109  
110 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
111 0 my $s2k = shift;
112 0 my $buf = Crypt::OpenPGP::Buffer->new;
113 0 $buf->put_int8(2);
114 0 $buf->put_int8($s2k->{hash_alg});
115 0 $buf->put_bytes($s2k->{salt});
116 0 $buf->bytes;
117 }
118  
119 package Crypt::OpenPGP::S2k::Salt_Iter;
120 8 ▶use parent qw( Crypt::OpenPGP::S2k );
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 8 ▶use Crypt::OpenPGP::Constants qw( DEFAULT_DIGEST );
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 9 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
125 9 my $s2k = shift;
126 9 my($buf) = @_;
127 9 ▶ if ($buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf) { } 5 4 9
128 5 $s2k->{hash_alg} = $buf->get_int8;
129 5 $s2k->{salt} = $buf->get_bytes(8);
130 5 $s2k->{count} = $buf->get_int8;
131 }
132 else {
133 4 $s2k->{hash_alg} = DEFAULT_DIGEST;
134 4 $s2k->{salt} = Crypt::OpenPGP::Util::get_random_bytes(8);
135 4 $s2k->{count} = 96;
136 }
137 9 ▶ if ($s2k->{hash_alg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2k->{"hash_alg"}) 9 0 9
138 9 $s2k->{hash} = Crypt::OpenPGP::Digest->new($s2k->{hash_alg});
139 }
140 9 $s2k;
141 }
142  
143 14 ▶sub s2k {
Subroutine s2k: called CC=4 SCAR=14.5
144 14 my $s2k = shift;
145 14 my($pass, $pad) = @_;
146 14 my $salt = $s2k->{salt};
147 14 my $count = (16 + ($s2k->{count} & 15)) << (($s2k->{count} >> 4) + 6);
148 14 my $len = length($pass) + 8;
149 14 ▶ if ($count < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $len) 0 14 14
150 0 $count = $len;
151 }
152 14 my $res = $pad;
153 14 ▶ while ($count > $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $len) 74894 14 74908
154 74894 $res .= $salt . $pass;
155 74894 $count -= $len;
156 }
157 14 ▶ if ($count < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 8) { } 14 0 14
158 14 $res .= substr($salt, 0, $count);
159 } else {
160 0 $res .= $salt;
161 0 $count -= 8;
162 0 $res .= substr($pass, 0, $count);
163 }
164 14 $s2k->{hash}->hash($res);
165 }
166  
167 2 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
168 2 my $s2k = shift;
169 2 my $buf = Crypt::OpenPGP::Buffer->new;
170 2 $buf->put_int8(3);
171 2 $buf->put_int8($s2k->{hash_alg});
172 2 $buf->put_bytes($s2k->{salt});
173 2 $buf->put_int8($s2k->{count});
174 2 $buf->bytes;
175 }
176  
177 8 1;
178 __END__
« OpenPGP/Util.pm ↑ Summary OpenPGP/Config.pm »