lib/App/Greple/PgpDecryptor.pm

filter: Statementstmt 22.2% 24 / 108 Branchbran 3.1% 1 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.8% 7 / 22 Podpod 33.3% 3 / 9 totaltotal 20.4% 35 / 171 CC 17 SCAR 22.6
CC
17
Cov
18%
initialize
30.0
decrypt
15.4
reset
12.0
CRAP 9.5
« lib/App/Greple/Regions.pm ↑ Summary lib/App/Greple/perl.pm »
1 =head1 NAME
2  
3 PgpDecryptor - Module for decrypt PGP data
4  
5 =head1 SYNOPSIS
6  
7 my $pgp = App::Greple::PgpDecryptor->new;
8  
9 =head1 DESCRIPTION
10  
11 =head2 initialize
12  
13 Initialize object.
14  
15 Without parameter, read passphrase from terminal.
16  
17 $pgp->initialize();
18  
19 Provide passphrase string or file descriptor if available.
20  
21 $pgp->initialize({passphrase => passphrase});
22  
23 $pgp->initialize({passphrase_fd => fd});
24  
25 =head2 decrypt
26  
27 Decrypt data. Pass the encrypted data and get the result.
28  
29 $decrypted = $pgp->decript($encrpted);
30  
31 =head2 decrypt_comand
32  
33 Return decrypt command string. You can use this command to decrypt
34 data. Call B<reset> after command execution.
35  
36 open(STDIN, '-|') or exec $pgp->decrypt_command;
37  
38 =head2 reset
39  
40 Reset internal status.
41  
42 =cut
43  
44  
45 package App::Greple::PgpDecryptor;
46  
47 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my $obj = bless {
53 FH => undef,
54 }, shift;
55 0 $obj;
56 }
57  
58 0 ▶sub initialize {
Subroutine initialize: not called CC=5 SCAR=34.0
Pod documented
59 0 my $obj = shift;
60 0 ▶ my $opt = @_ ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
61 0 my $passphrase = "";
62  
63 0 ▶ if (my $fd = $opt->{passphrase_fd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fd = $opt->{'passphrase_fd'}) { } 0 0 0
64 0 $obj->fh(_openfh($fd));
65 }
66 else {
67 0 ▶ if (not defined $obj->fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->fh) 0 0 0
68 0 $obj->fh(_openfh());
69 }
70 0 ▶ if (defined $opt->{passphrase}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{'passphrase'}) { } 0 0 0
71 0 $passphrase = $opt->{passphrase};
72 } else {
73 0 _readphrase(\$passphrase);
74 }
75 0 $obj->setphrase(\$passphrase);
76  
77 ##
78 ## Destroy data as much as possible
79 ##
80 0 $passphrase =~ s/./\0/g;
81 0 $passphrase = "";
82 0 undef $passphrase;
83 }
84  
85 0 $obj;
86 }
87  
88 0 ▶sub setphrase {
Subroutine setphrase: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my $obj = shift;
90 0 my $fh = $obj->fh;
91 0 my $passphrase_r = shift;
92  
93 0 $obj->reset;
94 0 $fh->syswrite($$passphrase_r, length($$passphrase_r));
95 0 $obj->reset;
96 }
97  
98 0 ▶sub getphrase {
Subroutine getphrase: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $obj = shift;
100 0 my $fh = $obj->fh;
101  
102 0 $obj->reset;
103 0 my $phrase = $fh->getline;
104 0 $obj->reset;
105 0 $phrase;
106 }
107  
108 0 ▶sub fh {
Subroutine fh: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my $obj = shift;
110 @_ ? $obj->{FH} = shift
111 0 ▶ : $obj->{FH};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
112 }
113  
114 0 ▶sub pgppassfd {
Subroutine pgppassfd: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $obj = shift;
116 0 $obj->fh->fileno;
117 }
118  
119 0 ▶sub decrypt_command {
Subroutine decrypt_command: not called CC=1 SCAR=6.9
Pod undocumented
120 0 my $obj = shift;
121 0 my $command = "gpg";
122 0 my @option = ( qw(--quiet --batch --decrypt) ,
123 qw(--no-tty --no-mdc-warning) );
124 0 sprintf "$command @option --passphrase-fd %d", $obj->pgppassfd;
125 }
126  
127 0 ▶sub reset {
Subroutine reset: not called CC=3 SCAR=24.8
Pod documented
128 0 my $obj = shift;
129 0 ▶ defined $obj->fh or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $obj->fh or return 0 0 0
130 0 ▶ $obj->fh->sysseek(0, 0) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$obj->fh->sysseek(0, 0) or die $! 0 0 0
131 }
132  
133 0 ▶sub _openfh {
Subroutine _openfh: not called CC=4 SCAR=22.9
134 1 ▶ use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137 0 my $fd = shift;
138 0 my $fh;
139  
140 0 ▶ if (defined $fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fd) { } 0 0 0
141 0 $fh = IO::Handle->new;
142 0 $fh->fdopen($fd, "w+");
143 } else {
144 0 $fh = new_tmpfile IO::File;
145 0 ▶ defined $fh or die "new_tmpefile: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $fh or die "new_tmpefile: $!" 0 0 0
146 }
147  
148 0 ▶ $fh->fcntl(F_SETFD, 0) or die "fcntl F_SETFD failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->fcntl(2, 0) or die "fcntl F_SETFD failed: $!\n" 0 0 0
149  
150 0 return $fh;
151 }
152  
153 1 my $noecho;
154 my $restore;
155 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.6
156 1 ($noecho, $restore) = eval {
157 1 require Term::ReadKey;
158 0 import Term::ReadKey;
159 0 ▶ (sub { ReadMode('noecho', @_) }, sub { ReadMode('restore', @_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 };
161 1 ▶ if (not defined $noecho) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $noecho) 1 0 1
162 0 ▶ $noecho = sub { system 'stty -echo < /dev/tty' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
163 0 ▶ $restore = sub { system 'stty echo < /dev/tty' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
164 }
165 }
166  
167 0 ▶sub _readphrase {
Subroutine _readphrase: not called CC=3 SCAR=24.8
168 0 my $passphrase_r = shift;
169  
170 0 print STDERR "Enter PGP Passphrase> ";
171 0 ▶ open my $tty, '<', '/dev/tty' or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $tty, "<", "/dev/tty" or die 0 0 0
172 0 $noecho->($tty);
173 0 ▶ if (defined (my $pass = ReadLine(0, $tty))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pass = ReadLine(0, $tty))) 0 0 0
174 0 chomp($$passphrase_r = $pass);
175 }
176 0 $restore->($tty);
177 0 close $tty;
178 0 print STDERR "\n";
179  
180 0 $passphrase_r;
181 }
182  
183 0 ▶sub decrypt {
Subroutine decrypt: not called CC=4 SCAR=27.3
Pod documented
184 1 ▶ use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
185  
186 0 my $obj = shift;
187 0 my $enc_data = shift;
188  
189 0 $obj->reset;
190  
191 0 my $pid = open2(\*RDRFH, \*WTRFH, $obj->decrypt_command);
192  
193 0 ▶ if (length($enc_data) <= 1024 * 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $enc_data <= 16384) { } 0 0 0
194 0 print WTRFH $enc_data;
195 }
196 else {
197 0 my $pid = fork;
198 0 ▶ croak "process fork failed" if not defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
199 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
200 0 print WTRFH $enc_data;
201 0 close WTRFH;
202 0 close RDRFH;
203 0 exit;
204 }
205 }
206 0 close WTRFH;
207 0 my $dec_data = do { local $/ ; <RDRFH> };
208 0 close RDRFH;
209  
210 0 $dec_data;
211 }
212  
213 1 1;
« lib/App/Greple/Regions.pm ↑ Summary lib/App/Greple/perl.pm »