blib/lib/Secret/Simple.pm

filter: Statementstmt 80.7% 105 / 130 Branchbran 37.8% 25 / 66 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 90.0% 18 / 20 Podpod 100% 12 / 12 totaltotal 64.0% 162 / 253 CC 41 SCAR 31.6
CC
41
Cov
63%
_read_rawfile
110.0
keyfilesize
20.0
keydata
11.3
CRAP 23.6
↑ Summary
1 package Secret::Simple;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw( @ISA @EXPORT );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.11';
13  
14 3 @ISA = qw( Exporter );
15 3 @EXPORT = qw( ssdecrypt ssdecryptraw ssencrypt ssencryptraw );
16  
17 3 my $DEFAULT_CIPHER = 'Rijndael_PP';
18 3 my $DEFAULT_KEYFILE = '~/.ssh/id_dsa';
19 3 my $DEFAULT_GARBAGE = 'eLH6eDl7H+Ng07Zj';
20  
21 3 ▶sub new {
Subroutine new: called CC=9 SCAR=22.8
Pod documented
22 3 my ($class, @args) = @_;
23 3 my $self = {};
24  
25 3 my (%args, %option);
26 3 ▶ @args = ( 'key', '{sskeyfile}' ) unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 3 3
27 3 ▶ if (ref($args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 0 3 3
28 0 %option = %{$args[0]};
29 } else {
30 3 ▶ @args = ( 'key', $args[0] ) if @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 1 2 3
31 3 ▶ croak "Invalid arguments passed" if scalar(@args) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args & 1 0 3 3
32 3 %args = @args;
33 }
34 3 my %tmp = map { $_ => 1 } qw( key keyfilesize );
35 3 for my $opt (keys %args) {
36 3 my $opt2 = $opt;
37 3 $opt2 =~ s/^-//;
38 3 ▶ croak "Unrecognized -$opt2 option passed" unless $tmp{$opt2};
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp{$opt2} 0 3 3
39 3 $option{$opt2} = $args{$opt};
40 }
41 3 ▶ $option{key} = '{sskeyfile}' unless $option{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"key"} 0 3 3
42 3 key($self, $option{key});
43 3 $self->{keyfilesize} = 0;
44 3 ▶ keyfilesize($self, $option{keyfilesize}) if $option{keyfilesize};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"keyfilesize"} 0 3 3
45 3 $self->{keydata} = keydata($self);
46  
47 3 bless($self, $class);
48 3 return $self;
49 }
50  
51 1 ▶sub decrypt {
Subroutine decrypt: called CC=2 SCAR=7.0
Pod documented
52 1 my ($self, $b64ciphertext) = @_;
53 1 ▶ return unless $b64ciphertext;
Branch: 1 branch
BranchTrueFalseTotal
unless $b64ciphertext 0 1 1
54 1 my $ciphertext = decode_base64($b64ciphertext);
55 1 my $plaintext = decryptraw($self, $ciphertext);
56 1 return $plaintext;
57 }
58  
59 2 ▶sub decryptraw {
Subroutine decryptraw: called CC=2 SCAR=7.0
Pod documented
60 2 my ($self, $ciphertext) = @_;
61 2 ▶ return unless $ciphertext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ciphertext 0 2 2
62 my $cipher = Crypt::CBC->new(
63 -key => $self->{keydata},
64 2 -cipher => $DEFAULT_CIPHER,
65 -header => 'none',
66 -iv => $DEFAULT_GARBAGE
67 );
68 2 my $plaintext = $cipher->decrypt($ciphertext);
69 2 return $plaintext;
70 }
71  
72 1 ▶sub encrypt {
Subroutine encrypt: called CC=2 SCAR=7.0
Pod documented
73 1 my ($self, $plaintext) = @_;
74 1 my $ciphertext = encryptraw($self, $plaintext);
75 1 ▶ return unless $ciphertext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ciphertext 0 1 1
76 1 return encode_base64( $ciphertext );
77 }
78  
79 2 ▶sub encryptraw {
Subroutine encryptraw: called CC=2 SCAR=7.0
Pod documented
80 2 my ($self, $plaintext) = @_;
81 2 ▶ return unless $plaintext;
Branch: 1 branch
BranchTrueFalseTotal
unless $plaintext 0 2 2
82 my $cipher = Crypt::CBC->new(
83 -key => $self->{keydata},
84 2 -cipher => $DEFAULT_CIPHER,
85 -header => 'none',
86 -iv => $DEFAULT_GARBAGE
87 );
88 2 my $ciphertext = $cipher->encrypt($plaintext);
89 2 return $ciphertext;
90 }
91  
92 3 ▶sub key {
Subroutine key: called CC=4 SCAR=14.5
Pod documented
93 3 my ($self, $key) = @_;
94 3 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 3 0 3
95 3 ▶ croak "Bad key specification"
Branch: 1 branch
BranchTrueFalseTotal
if ref $key and ref $key ne "ARRAY" 0 3 3
Condition66%2/3
ref $key and ref $key ne "ARRAY"
!ll&&!rl&&r
120
MC/DC0%0/2
ref $key and ref $key ne "ARRAY"
ref $key ref $key ne "ARRAY"
Truth table66%2/3
A and B
A= ref $keyB= ref $key ne "ARRAY"
ABresult
0X0
100
111
96 if ref($key) && ref($key) ne 'ARRAY';
97 3 $self->{key} = $key;
98 }
99 3 return $self->{key};
100 }
101  
102 3 ▶sub keydata {
Subroutine keydata: called CC=7 SCAR=24.2
Pod documented
103 3 my ($self) = @_;
104  
105 3 ▶ unless (defined $self->{keydata}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"keydata"}) 3 0 3
106 # calculate aggregate key data
107 my @keys = ref($self->{key}) eq 'ARRAY' ?
108 3 ▶ @{$self->{key}} : ( $self->{key} );
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'key'} eq 'ARRAY' ? : 2 1 3
109 3 my $data = "";
110 3 for my $frag (@keys) {
111 3 my $piece = $frag;
112 3 ▶ if ($frag =~ /^\{sskeyfile\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frag =~ /^\{sskeyfile\}/) 0 3 3
113 0 my $fn = $frag;
114 0 $fn =~ s/^\{sskeyfile\}//;
115 0 ▶ $fn = $DEFAULT_KEYFILE unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
116 0 my ($fn1) = glob($fn);
117 0 ▶ croak "No access to specified key file '$fn'"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $fn1 0 0 0
118 unless -r $fn1;
119 0 $piece = _read_rawfile($fn1, $self->{keyfilesize});
120 }
121 3 $data .= $piece;
122 }
123 3 $self->{keydata} = $data;
124 }
125  
126 3 return $self->{keydata};
127 }
128  
129 0 ▶sub keyfilesize {
Subroutine keyfilesize: not called CC=4 SCAR=30.0
Pod documented
130 0 my ($self, $num) = @_;
131 0 ▶ croak "Bad limit passed" if defined $num && $num !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $num and not $num =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $num and not $num =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $num and not $num =~ /^\d+$/
defined $num not $num =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= defined $numB= not $num =~ /^\d+$/
ABresult
0X0
100
111
132 0 ▶ $self->{keyfilesize} = $num if defined $num;
Branch: 1 branch
BranchTrueFalseTotal
if defined $num 0 0 0
133 0 return $self->{keyfilesize};
134 }
135  
136 # The procedural style function section begins here.
137  
138 1 ▶sub ssdecrypt {
Subroutine ssdecrypt: called CC=2 SCAR=7.0
Pod documented
139 1 my ($b64ciphertext, @keyspec) = @_;
140 1 ▶ return unless $b64ciphertext;
Branch: 1 branch
BranchTrueFalseTotal
unless $b64ciphertext 0 1 1
141 1 my $ciphertext = decode_base64($b64ciphertext);
142 1 my $plaintext = ssdecryptraw($ciphertext, @keyspec);
143 1 return $plaintext;
144 }
145  
146 1 ▶sub ssdecryptraw {
Subroutine ssdecryptraw: called CC=3 SCAR=11.3
Pod documented
147 1 my ($ciphertext, @keyspec) = @_;
148 1 ▶ return unless $ciphertext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ciphertext 0 1 1
149 1 ▶ my $ss = @keyspec ?
Branch: 1 branch
BranchTrueFalseTotal
@keyspec ? : 1 0 1
150 Secret::Simple->new( key => [ @keyspec ] ) :
151 Secret::Simple->new();
152 1 my $plaintext = $ss->decryptraw($ciphertext);
153 1 return $plaintext;
154 }
155  
156 1 ▶sub ssencrypt {
Subroutine ssencrypt: called CC=3 SCAR=11.3
Pod documented
157 1 my ($plaintext, @keyspec) = @_;
158 1 ▶ return unless $plaintext;
Branch: 1 branch
BranchTrueFalseTotal
unless $plaintext 0 1 1
159 1 my $ciphertext = ssencryptraw($plaintext, @keyspec);
160 1 ▶ return unless $ciphertext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ciphertext 0 1 1
161 1 return encode_base64( $ciphertext );
162 }
163  
164 1 ▶sub ssencryptraw {
Subroutine ssencryptraw: called CC=3 SCAR=11.3
Pod documented
165 1 my ($plaintext, @keyspec) = @_;
166 1 ▶ return unless $plaintext;
Branch: 1 branch
BranchTrueFalseTotal
unless $plaintext 0 1 1
167 1 ▶ my $ss = @keyspec ?
Branch: 1 branch
BranchTrueFalseTotal
@keyspec ? : 1 0 1
168 Secret::Simple->new( key => [ @keyspec ] ) :
169 Secret::Simple->new();
170 1 my $ciphertext = $ss->encryptraw($plaintext);
171 1 return $ciphertext;
172 }
173  
174 # The private module function section begins here.
175  
176 # The _read_rawfile private function accepts a filename and an optional
177 # limit argument. The entire contents of a specified file will be read
178 # and returned as a string if the limit is undefined or zero, but a
179 # maximum of $limit bytes will be read in and returned otherwise.
180  
181 0 ▶sub _read_rawfile {
Subroutine _read_rawfile: not called CC=10 SCAR=47.0
182 0 my ($fn, $limit) = @_;
183 0 ▶ croak "No filename argument passed" unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
184 0 ▶ croak "Bad limit passed" if $limit && $limit !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and not $limit =~ /^\d+$/ 0 0 0
Condition0%0/3
$limit and not $limit =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and not $limit =~ /^\d+$/
$limit not $limit =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= $limitB= not $limit =~ /^\d+$/
ABresult
0X0
100
111
185 0 my ($chunk, $num, $data, $buf) = ( 8192, 0, "" );
186 0 ▶ croak "Unable to read from file" unless
Branch: 1 branch
BranchTrueFalseTotal
unless open my $F, $fn 0 0 0
187 open my ($F), $fn;
188 0 binmode($F);
189 0 ▶ until ( eof($F) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $F) 0 0 0
190 0 ▶ $chunk = $limit - $num if $limit && $num + $chunk > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $num + $chunk > $limit 0 0 0
Condition0%0/3
$limit and $num + $chunk > $limit
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $num + $chunk > $limit
$limit $num + $chunk > $limit
Truth table0%0/3
A and B
A= $limitB= $num + $chunk > $limit
ABresult
0X0
100
111
191 0 $num += read($F, $buf, $chunk);
192 0 $data .= $buf;
193 0 ▶ last if $limit && $num >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $num >= $limit 0 0 0
Condition0%0/3
$limit and $num >= $limit
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $num >= $limit
$limit $num >= $limit
Truth table0%0/3
A and B
A= $limitB= $num >= $limit
ABresult
0X0
100
111
194 }
195 0 close $F;
196 0 return $data;
197 }
198  
199 3 1;
200 __END__
↑ Summary