Usul/Crypt/Util.pm

filter: Statementstmt 89.6% 52 / 58 Branchbran 75.0% 15 / 20 Conditioncond 62.9% 17 / 27 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 87.5% 14 / 16 Podpod 100% 4 / 4 totaltotal 77.5% 107 / 138 CC 21 SCAR 26.6
CC
21
Cov
80%
__ANON__
31.7
__ANON__
4.0
__ANON__
2.0
CRAP 14.3
« Usul/IPC/Cmd.pm ↑ Summary Usul/File.pm »
1 package Class::Usul::Crypt::Util;
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  
6 3 ▶use Class::Usul::Constants qw( FALSE NUL TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Class::Usul::Crypt qw( decrypt default_cipher encrypt );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Class::Usul::Functions qw( merge_attributes throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter 5.57 qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::DataClass::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our @EXPORT_OK = qw( decrypt_from_config encrypt_for_config
14 get_cipher is_encrypted );
15  
16 3 my $_args_cache = {};
17  
18 # Private functions
19 5 ▶my $_extract_crypt_params = sub { # Returns cipher and encrypted text
Subroutine __ANON__: called CC=4 SCAR=13.9
20 # A single scalar arg not matching the pattern is just a cipher
21 # It really is better this way round. Leave it alone
22 5 ▶ return $_[ 0 ] && $_[ 0 ] =~ m{ \A [{] (.+) [}] (.*) \z }mx
Branch: 2 branches
BranchTrueFalseTotal
$_[0] && $_[0] =~ / \A [{] (.+) [}] (.*) \z /mx ? : 2 3 5
$_[0] ? : 2 1 3
Condition100%3/3
$_[0] and $_[0] =~ / \A [{] (.+) [}] (.*) \z /mx
!ll&&!rl&&r
122
MC/DC100%2/2
$_[0] and $_[0] =~ / \A [{] (.+) [}] (.*) \z /mx
$_[0] $_[0] =~ / \A [{] (.+) [}] (.*) \z /mx
Truth table100%3/3
A and B
A= $_[0]B= $_[0] =~ / \A [{] (.+) [}] (.*) \z /mx
ABresult
0X0
100
111
23 ? ($1, $2) : $_[ 0 ] ? ($_[ 0 ]) : (default_cipher, $_[ 0 ]);
24 3 };
25  
26 4 ▶my $_get_crypt_args = sub { # Sets cipher, salt, and seed keys in args hash
Subroutine __ANON__: called CC=15 SCAR=34.6
27 4 my ($config, $cipher) = @_; my $params = {};
28  
29 # Works if config is an object or a hash
30 4 merge_attributes $params, $config,
31 [ qw( ctrldir prefix read_secure salt seed seed_file ) ];
32  
33 my $args = { cipher => $cipher,
34 4 ▶ salt => $params->{salt} // $params->{prefix} // NUL };
Condition80%4/5
($params->{"salt"} // $params->{"prefix"}) // ""
l!l
13
$params->{"salt"} // $params->{"prefix"}
l!l&&r!l&&!r
103
MC/DC50%1/2
($params->{"salt"} // $params->{"prefix"}) // ""
$params->{"salt"} $params->{"prefix"}
Truth table66%2/3
A // B
A= $params->{"salt"}B= $params->{"prefix"}
ABresult
1X1
011
000
35 4 ▶ my $file = $params->{seed_file} // $params->{prefix} // 'seed';
Condition80%4/5
($params->{"seed_file"} // $params->{"prefix"}) // "seed"
l!l
13
$params->{"seed_file"} // $params->{"prefix"}
l!l&&r!l&&!r
013
MC/DC50%1/2
($params->{"seed_file"} // $params->{"prefix"}) // "seed"
$params->{"seed_file"} $params->{"prefix"}
Truth table66%2/3
A // B
A= $params->{"seed_file"}B= $params->{"prefix"}
ABresult
1X1
011
000
36  
37 0 ▶ if ($params->{seed}) { $args->{seed} = $params->{seed} }
Branch: 3 branches
BranchTrueFalseTotal
if ($params->{'seed'}) { } 0 4 4
elsif (defined $_args_cache->{$file}) { } 0 4 4
elsif ($params->{'read_secure'}) { } 0 4 4
38 elsif (defined $_args_cache->{ $file }) {
39 0 $args->{seed} = $_args_cache->{ $file };
40 }
41 elsif ($params->{read_secure}) {
42 0 my $cmd = $params->{read_secure}." ${file}";
43  
44 0 ▶ try { $args->{seed} = $_args_cache->{ $file } = qx( $cmd ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 ▶ catch { throw "Reading secure file ${file}: ${_}" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 }
47 else {
48 4 my $path = io $file;
49  
50 $path->exists and ($path->stat->{mode} & 0777) == 0600
51 4 ▶ and $args->{seed} = $_args_cache->{ $file } = $path->all;
Branch: 1 branch
BranchTrueFalseTotal
$path->exists and ($path->stat->{"mode"} & 511) == 384 and $args->{"seed"} = $_args_cache->{$file} = $path->all 0 4 4
Condition33%1/3
$path->exists and ($path->stat->{"mode"} & 511) == 384
!ll&&!rl&&r
400
MC/DC0%0/2
$path->exists and ($path->stat->{"mode"} & 511) == 384
$path->exists ($path->stat->{"mode"} & 511) == 384
Truth table33%1/3
A and B
A= $path->existsB= ($path->stat->{"mode"} & 511) == 384
ABresult
0X0
100
111
52  
53 not $args->{seed}
54 and $path = io( [ $params->{ctrldir} // NUL, "${file}.key" ] )
55 and $path->exists and ($path->stat->{mode} & 0777) == 0600
56 4 ▶ and $args->{seed} = $_args_cache->{ $file } = $path->all;
Branch: 1 branch
BranchTrueFalseTotal
not $args->{"seed"} and $path = &io([$params->{"ctrldir"} // "", "$file.key"]) and $path->exists and ($path->stat->{"mode"} & 511) == 384 and $args->{"seed"} = $_args_cache->{$file} = $path->all 0 4 4
Condition45%5/11
not $args->{"seed"} and $path = &io([$params->{"ctrldir"} // "", "$file.key"]) and $path->exists and ($path->stat->{"mode"} & 511) == 384
!ll&&!rl&&r
400
not $args->{"seed"} and $path = &io([$params->{"ctrldir"} // "", "$file.key"]) and $path->exists
!ll&&!rl&&r
040
not $args->{"seed"} and $path = &io([$params->{"ctrldir"} // "", "$file.key"])
!ll&&!rl&&r
040
$params->{"ctrldir"} // ""
l!l
22
MC/DC0%0/4
not $args->{"seed"} and $path = &io([$params->{"ctrldir"} // "", "$file.key"]) and $path->exists and ($path->stat->{"mode"} & 511) == 384
not $args->{"seed"} $path = &io([$params->{"ctrldir"} // "", "$file.key"]) $path->exists ($path->stat->{"mode"} & 511) == 384
MC/DC100%1/1
$params->{"ctrldir"} // ""
$params->{"ctrldir"}
Truth table0%0/5
not(A) and B and C and D
A= not $args->{"seed"}B= $path = &io([$params->{"ctrldir"} // "", "$file.key"])C= $path->existsD= ($path->stat->{"mode"} & 511) == 384
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table100%2/2
A
A= $params->{"ctrldir"}
Aresult
11
00
57 }
58  
59 4 return $args;
60 3 };
61  
62 # Public functions
63 3 ▶sub decrypt_from_config ($$) {
Subroutine decrypt_from_config: called CC=2 SCAR=6.9
Pod documented
64 3 my ($config, $encrypted) = @_;
65  
66 3 my ($cipher, $cipher_text) = $_extract_crypt_params->( $encrypted );
67 3 my $args = $_get_crypt_args->( $config, $cipher );
68  
69 3 ▶ return $cipher_text ? decrypt $args, $cipher_text : $encrypted;
Branch: 1 branch
BranchTrueFalseTotal
$cipher_text ? : 1 2 3
70 }
71  
72 2 ▶sub encrypt_for_config ($$;$) {
Subroutine encrypt_for_config: called CC=2 SCAR=6.9
Pod documented
73 2 my ($config, $plain_text, $encrypted) = @_;
74  
75 2 ▶ $plain_text or return $plain_text;
Branch: 1 branch
BranchTrueFalseTotal
$plain_text or return $plain_text 1 1 2
76  
77 1 my ($cipher) = $_extract_crypt_params->( $encrypted );
78 1 my $args = $_get_crypt_args->( $config, $cipher );
79  
80 1 return "{${cipher}}".(encrypt $args, $plain_text);
81 }
82  
83 1 ▶sub get_cipher ($) {
Subroutine get_cipher: called CC=1 SCAR=0.0
Pod documented
84 1 my ($cipher) = $_extract_crypt_params->( $_[ 0 ] ); return $cipher;
85 }
86  
87 2 ▶sub is_encrypted ($) {
Subroutine is_encrypted: called CC=2 SCAR=6.9
Pod documented
88 2 ▶ return $_[ 0 ] =~ m{ \A [{] .+ [}] .* \z }mx ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ / \A [{] .+ [}] .* \z /mx ? : 1 1 2
89 }
90  
91 3 1;
92  
93 __END__
« Usul/IPC/Cmd.pm ↑ Summary Usul/File.pm »