Encrypted.pm

filter: Statementstmt 29.3% 32 / 109 Branchbran 12.9% 8 / 62 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 3 totaltotal 20.4% 45 / 220 CC 41 SCAR 45.2
CC
41
Cov
21%
DESTROY
210.0
encrypted
90.0
new
56.0
CRAP 92.1
↑ Summary example.pl »
1 package Data::Encrypted;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $VAR1);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7  
8 1 require Crypt::RSA;
9 1 require Crypt::RSA::Key::Public::SSH;
10 1 require Crypt::RSA::Key::Private::SSH;
11  
12 1 require Term::ReadPassword;
13 1 require File::HomeDir;
14 1 require Storable;
15 1 require Fcntl;
16  
17 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $VERSION = '0.07';
20 1 @ISA = qw(Exporter);
21 1 @EXPORT = qw();
22 1 @EXPORT_OK = qw(encrypted encrypt finish finished);
23  
24 1 my $RESET = 0;
25 1 my ($FILE, $FH);
26 1 my ($PK, $SK, $PW);
27  
28 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
29  
30 1 @_ = _get_args(@_);
31  
32 # let Exporter finish it's work:
33 1 Data::Encrypted->export_to_level(1, @_);
34 }
35  
36 1 ▶sub _get_args {
Subroutine _get_args: called CC=8 SCAR=31.9
37  
38 1 ▶ for (my $i = 0 ; $i < @_ ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 1 1 2
39 1 ▶ if ($_[$i] =~ m/-?reset/oi) {
Branch: 6 branches
BranchTrueFalseTotal
if ($_[$i] =~ /-?reset/io) { } 0 1 1
elsif ($_[$i] =~ /-?file/io) { } 0 1 1
elsif ($_[$i] =~ /-?fh/io) { } 0 1 1
elsif ($_[$i] =~ /-?pk/io) { } 0 1 1
elsif ($_[$i] =~ /-?sk/io) { } 0 1 1
elsif ($_[$i] =~ /-?pw/io) { } 0 1 1
40 0 $RESET = 1;
41 0 splice(@_, $i--, 1);
42 } elsif ($_[$i] =~ m/-?file/oi) {
43 0 $FILE = $_[$i+1];
44 0 splice(@_, $i--, 2);
45 0 _get_filehandle();
46 } elsif ($_[$i] =~ m/-?fh/oi) {
47 0 $FH = $_[$i+1];
48 0 splice(@_, $i--, 2);
49 } elsif ($_[$i] =~ m/-?pk/oi) {
50 0 $PK = $_[$i+1];
51 0 splice(@_, $i--, 2);
52 } elsif ($_[$i] =~ m/-?sk/oi) {
53 0 $SK = $_[$i+1];
54 0 splice(@_, $i--, 2);
55 } elsif ($_[$i] =~ m/-?pw/oi) {
56 0 $PW = $_[$i+1];
57 0 splice(@_, $i--, 2);
58 }
59 }
60  
61 1 return @_;
62 }
63  
64 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
65  
66 0 my $class = shift;
67 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
68 0 my $self = bless {}, $class;
69  
70 0 _get_args(@_);
71  
72 0 ▶ unless (defined $FH) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $FH) 0 0 0
73 0 _get_filehandle()
74 }
75  
76 0 ▶ $self->{_public} = ref $PK ? $PK : new Crypt::RSA::Key::Public::SSH
Branch: 1 branch
BranchTrueFalseTotal
ref $PK ? : 0 0 0
Condition0%0/2
$PK || &File::HomeDir::home() . '/.ssh/identity.pub'
l!l
00
MC/DC0%0/1
$PK || &File::HomeDir::home() . '/.ssh/identity.pub'
$PK
Truth table0%0/2
A
A= $PK
Aresult
11
00
77 Filename => $PK || (File::HomeDir::home() . '/.ssh/identity.pub');
78 0 ▶ $self->{_private} = ref $SK ? $SK : new Crypt::RSA::Key::Private::SSH
Branch: 1 branch
BranchTrueFalseTotal
ref $SK ? : 0 0 0
Condition0%0/2
$SK || &File::HomeDir::home() . '/.ssh/identity'
l!l
00
MC/DC0%0/1
$SK || &File::HomeDir::home() . '/.ssh/identity'
$SK
Truth table0%0/2
A
A= $SK
Aresult
11
00
79 (Filename => $SK || (File::HomeDir::home() . '/.ssh/identity'),
80 Password => $PW );
81  
82 0 $self->{_rsa} = new Crypt::RSA;
83  
84 0 return $self;
85 }
86  
87 0 ▶sub _get_filehandle {
Subroutine _get_filehandle: not called CC=6 SCAR=37.4
88  
89 0 ▶ if ($FILE =~ m/^~([^\/]*)\//o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FILE =~ m[^~([^/]*)/]o) 0 0 0
90 0 my $user = $1;
91 0 my $path;
92 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
93 0 $path = File::HomeDir::home($user);
94 } else {
95 0 $path = File::HomeDir::home();
96 }
97 0 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
98 0 croak "No home directory for user: $user\n";
99 }
100 0 $FILE =~ s/^~$user/$path/;
101 }
102  
103 0 ▶ sysopen(FH, $FILE, Fcntl::O_RDWR() | Fcntl::O_CREAT() )
Branch: 1 branch
BranchTrueFalseTotal
sysopen FH, $FILE, 66 or croak("Can't open ${FILE}: $!") 0 0 0
104 or croak "Can't open $FILE: $!";
105 0 ▶ flock(FH, Fcntl::LOCK_EX())
Branch: 1 branch
BranchTrueFalseTotal
flock FH, 2 or croak("Can't flock file: $!") 0 0 0
106 or croak "Can't flock file: $!";
107 0 $FH = \*FH;
108 }
109  
110 0 ▶sub encrypted {
Subroutine encrypted: not called CC=9 SCAR=45.0
Pod undocumented
111  
112 0 my $self = shift;
113 0 my $arg = shift;
114  
115 0 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
116 0 ▶ my $newself = Data::Encrypted->new(defined $arg ? $arg : (), @_);
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 0 0
117 0 ($self, $arg) = ($newself, $self);
118 }
119  
120 0 ▶ unless ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg) 0 0 0
121 0 croak "Must supply argument to encrypted!";
122 }
123  
124 0 ▶ unless (exists $self->{_data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_data"}) 0 0 0
125 0 $self->{_data} = '';
126 0 ▶ while (<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 0 0 0
127 0 $self->{_data} .= $_;
128 }
129
130 0 $self->{_data} =~ s/\s*$//os;
131  
132 0 ▶ if ($self->{_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_data'}) { } 0 0 0
133 $self->{_data} = $self->{_rsa}->decrypt(Cyphertext => $self->{_data},
134 Key => $self->{_private},
135 0 Armour => 1
136 );
137 0 $self->{_data} = Storable::thaw($self->{_data});
138 } else {
139 0 $self->{_data} = {};
140 }
141 }
142  
143 0 ▶ if ($RESET || ! exists $self->{_data}->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RESET or not exists $self->{"_data"}{$arg}) 0 0 0
Condition0%0/3
$RESET or not exists $self->{"_data"}{$arg}
l!l&&r!l&&!r
000
MC/DC0%0/2
$RESET or not exists $self->{"_data"}{$arg}
$RESET not exists $self->{"_data"}{$arg}
Truth table0%0/3
A or not(B)
A= $RESETB= not exists $self->{"_data"}{$arg}
ABresult
1X1
011
000
144 0 $self->{_data}->{$arg} =
145 Term::ReadPassword::read_password(ref($self) .
146 " value for '$arg' not found," .
147 " please enter: ", 0, 1, 1);
148 }
149 0 return $self->{_data}->{$arg};
150 }
151
152 1 *encrypt = \&encrypted;
153  
154 0 ▶sub finished {
Subroutine finished: not called CC=2 SCAR=17.9
Pod undocumented
155  
156 0 ▶ if ($FH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FH) 0 0 0
157 0 flock($FH, Fcntl::LOCK_UN());
158 0 close($FH);
159 0 undef $FH;
160 }
161 }
162  
163 1 *finish = \&finished;
164  
165 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=14 SCAR=53.5
166  
167 0 my $self = shift;
168  
169 0 ▶ if ($self && ($FH || $FILE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $FH || $FILE) 0 0 0
Condition0%0/6
$self and $FH || $FILE
!ll&&!rl&&r
000
$FH or $FILE
l!l&&r!l&&!r
000
MC/DC0%0/3
$self and $FH || $FILE
$self $FH $FILE
Truth table0%0/4
A and B or C
A= $selfB= $FHC= $FILE
ABCresult
0XX0
1000
11X1
1011
170 0 ▶ unless ($FH) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($FH) 0 0 0
171 0 ▶ sysopen(FH, $FILE, Fcntl::O_RDWR() | Fcntl::O_CREAT())
Branch: 1 branch
BranchTrueFalseTotal
sysopen FH, $FILE, 66 or croak("Can't open ${FILE}: $!") 0 0 0
172 or croak "Can't open $FILE: $!";
173 0 ▶ flock(FH, Fcntl::LOCK_EX())
Branch: 1 branch
BranchTrueFalseTotal
flock FH, 2 or croak("Can't flock file: $!") 0 0 0
174 or croak "Can't flock file: $!";
175 0 $FH = \*FH;
176 }
177 0 seek($FH, 0, 0);
178 0 ▶ unless ($RESET) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($RESET) 0 0 0
179 # need to rebuild our cryptosystem, ugh.
180 0 ▶ $self->{_public} = ref $PK ? $PK : new Crypt::RSA::Key::Public::SSH
Branch: 1 branch
BranchTrueFalseTotal
ref $PK ? : 0 0 0
Condition0%0/2
$PK || &File::HomeDir::home() . '/.ssh/identity.pub'
l!l
00
MC/DC0%0/1
$PK || &File::HomeDir::home() . '/.ssh/identity.pub'
$PK
Truth table0%0/2
A
A= $PK
Aresult
11
00
181 Filename => $PK || (File::HomeDir::home() . '/.ssh/identity.pub');
182 0 ▶ $self->{_private} = ref $SK ? $SK : new Crypt::RSA::Key::Private::SSH
Branch: 1 branch
BranchTrueFalseTotal
ref $SK ? : 0 0 0
Condition0%0/2
$SK || &File::HomeDir::home() . '/.ssh/identity'
l!l
00
MC/DC0%0/1
$SK || &File::HomeDir::home() . '/.ssh/identity'
$SK
Truth table0%0/2
A
A= $SK
Aresult
11
00
183 (Filename => $SK || (File::HomeDir::home() . '/.ssh/identity'),
184 Password => $PW );
185 0 $self->{_rsa} = new Crypt::RSA;
186 0 $self->{_data} = Storable::freeze($self->{_data});
187 $self->{_data} = $self->{_rsa}->encrypt(Message => $self->{_data},
188 Key => $self->{_public},
189 0 Armour => 1
190 );
191 0 print $FH $self->{_data};
192 }
193 0 delete $self->{_data};
194 }
195  
196 0 ▶ unless (exists $INC{'Inline/Files.pm'} || ! $FILE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{"Inline/Files.pm"} or not $FILE) 0 0 0
Condition0%0/3
exists $INC{"Inline/Files.pm"} or not $FILE
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $INC{"Inline/Files.pm"} or not $FILE
exists $INC{"Inline/Files.pm"} not $FILE
Truth table0%0/3
A or not(B)
A= exists $INC{"Inline/Files.pm"}B= not $FILE
ABresult
1X1
011
000
197 0 flock($FH, Fcntl::LOCK_UN());
198 0 close($FH);
199 0 undef $FH;
200 }
201  
202 }
203  
204 1 1;
205  
206 __END__
↑ Summary example.pl »