EncodedColumn/Crypt/OpenPGP.pm

filter: Statementstmt 79.6% 43 / 54 Branchbran 50.0% 10 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 2 totaltotal 68.4% 65 / 95 CC 14 SCAR 17.5
CC
14
Cov
70%
decrypt_pgp_rijndael256
12.0
_get_pgp_obj_from_args
6.8
make_encode_sub
6.3
CRAP 5.8
« EncodedColumn/Digest.pm ↑ Summary EncodedColumn/Crypt.pm »
1 package DBIx::Class::EncodedColumn::Crypt::OpenPGP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Crypt::OpenPGP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.01';
10  
11 =head1 NAME
12  
13 DBIx::Class::EncodedColumn::Crypt::OpenPGP - Encrypt columns using Crypt::OpenPGP
14  
15 =head1 SYNOPSIS
16  
17 __PACKAGE__->add_columns(
18 'secret_data' => {
19 data_type => 'TEXT',
20 encode_column => 1,
21 encode_class => 'Crypt::OpenPGP',
22 encode_args => {
23 recipient => '7BEF6294',
24 },
25 encode_check_method => 'decrypt_data',
26 };
27  
28 my $row = $schema->resultset('EncryptedClass')
29 ->create({ secret_data => 'This is secret' });
30  
31 is(
32 $row->decrypt_data('Private Key Passphrase'),
33 'This is secret',
34 'PGP/GPG Encryption works!'
35 );
36  
37 =head1 DESCRIPTION
38  
39 This is a conduit to working with L<Crypt::OpenPGP>, so that you can encrypt
40 data in your database using gpg. Currently this module only handles encrypting
41 but it may add signing of columns in the future
42  
43 =head1 CONFIGURATION
44  
45 In the column definition, specify the C<encode_args> hash as listed in the
46 synopsis. The C<recipient> is required if doing key exchange encryption, or
47 if you want to use symmetric key encryption using a passphrase you can
48 specify a C<passphrase> option:
49  
50 encode_args => { passphrase => "Shared Secret" }
51  
52 If you have a separate path to your public and private key ring file, or if you
53 have alternative L<Crypt::OpenPGP> configuration, you can specify the
54 constructor args using the C<pgp_args> configuration key:
55
56 encode_args => {
57 pgp_args => {
58 SecRing => "$FindBin::Bin/var/secring.gpg",
59 PubRing => "$FindBin::Bin/var/pubring.gpg",
60 }
61 }
62  
63 The included tests cover good usage, and it is advised to briefly browse through
64 them.
65  
66 Also, remember to keep your private keys secure!
67  
68 =cut
69  
70 1 my %VALID_ENCODE_ARGS = (
71 'compat' => 'Compat',
72 'cipher' => 'Cipher',
73 'compress' => 'Compress',
74 'mdc' => 'MDC',
75 );
76  
77 sub make_encode_sub {
78 4 ▶ my ( $class, $col, $args ) = @_;
Subroutine make_encode_sub: called CC=6 SCAR=18.4
Pod undocumented
79  
80 4 my ( $method, $method_arg );
81  
82 4 ▶ my $armour = defined $args->{armour} ? $args->{armour} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'armour'} ? : 4 0 4
83 4 ▶ if ( defined $args->{passphrase} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{'passphrase'}) { } 2 2 4
elsif (defined $args->{'recipient'}) { } 2 0 2
84 2 $method = 'Passphrase';
85 2 $method_arg = $args->{passphrase};
86 } elsif ( defined $args->{recipient} ) {
87 2 $method = 'Recipients';
88 2 $method_arg = $args->{recipient};
89 }
90  
91 4 my @other;
92 4 for my $opt (keys %VALID_ENCODE_ARGS) {
93 16 ▶ if ( defined $args->{$opt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$opt}) 1 15 16
94 1 push @other, $VALID_ENCODE_ARGS{$opt} => $args->{$opt};
95 }
96 }
97  
98 4 my $pgp = _get_pgp_obj_from_args($args);
99  
100 my $encoder = sub {
101 1 ▶ my ( $plain_text, $settings ) = @_;
Subroutine __ANON__: called CC=2 SCAR=11.6
102 1 my $val = $pgp->encrypt(
103 Data => $plain_text,
104 $method => $method_arg,
105 Armour => $armour,
106 @other,
107 );
108 0 ▶ croak "Unable to encrypt $col; check $method parameter (is $method_arg) (and that the key is known)" unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
109 0 return $val;
110 4 };
111 4 return $encoder;
112 }
113  
114 sub make_check_sub {
115 4 ▶ my ( $class, $col, $args ) = @_;
Subroutine make_check_sub: called CC=1 SCAR=4.1
Pod undocumented
116  
117 4 my $pgp = _get_pgp_obj_from_args($args);
118  
119 return sub {
120 0 ▶ my ( $self, $passphrase ) = @_;
Subroutine decrypt_pgp_rijndael256: not called CC=3 SCAR=24.8
121 0 my $text = $self->get_column($col);
122 0 my @res;
123 0 ▶ if ( defined $passphrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $passphrase) { } 0 0 0
124 0 @res = $pgp->decrypt( Passphrase => $passphrase, Data => $text );
125 } else {
126 0 @res = $pgp->decrypt( Data => $text );
127 }
128 0 ▶ croak $pgp->errstr unless $res[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $res[0] 0 0 0
129  
130 # Handle additional stuff in $res[1] and [2]?
131 0 return $res[0];
132 4 };
133 }
134  
135 sub _get_pgp_obj_from_args {
136 8 ▶ my ( $args ) = @_;
Subroutine _get_pgp_obj_from_args: called CC=6 SCAR=19.2
137 8 my $pgp;
138 8 ▶ if ( $args->{pgp_args} and ref $args->{pgp_args} eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'pgp_args'} and ref $args->{'pgp_args'} eq 'HASH') { } 6 2 8
elsif ($args->{'pgp_object'} and $args->{'pgp_object'}->isa('Crypt::OpenPGP')) { } 0 2 2
Condition66%2/3
$args->{"pgp_args"} and ref $args->{"pgp_args"} eq "HASH"
!ll&&!rl&&r
206
MC/DC50%1/2
$args->{"pgp_args"} and ref $args->{"pgp_args"} eq "HASH"
$args->{"pgp_args"} ref $args->{"pgp_args"} eq "HASH"
Truth table66%2/3
A and B
A= $args->{"pgp_args"}B= ref $args->{"pgp_args"} eq "HASH"
ABresult
0X0
100
111
139 6 $pgp = Crypt::OpenPGP->new( %{ $args->{pgp_args} } );
140 }
141 ▶ elsif ( $args->{pgp_object} and
Condition33%1/3
$args->{"pgp_object"} and $args->{"pgp_object"}->isa("Crypt::OpenPGP")
!ll&&!rl&&r
200
MC/DC0%0/2
$args->{"pgp_object"} and $args->{"pgp_object"}->isa("Crypt::OpenPGP")
$args->{"pgp_object"} $args->{"pgp_object"}->isa("Crypt::OpenPGP")
Truth table33%1/3
A and B
A= $args->{"pgp_object"}B= $args->{"pgp_object"}->isa("Crypt::OpenPGP")
ABresult
0X0
100
111
142 $args->{pgp_object}->isa('Crypt::OpenPGP')
143 ) {
144 0 $pgp = $args->{pgp_object};
145 } else {
146 2 $pgp = Crypt::OpenPGP->new;
147 }
148 8 ▶ croak "Unable to get initialize a Crypt::OpenPGP object" unless $pgp;
Branch: 1 branch
BranchTrueFalseTotal
unless $pgp 0 8 8
149  
150 8 return $pgp;
151 }
152  
153 =head1 AUTHOR
154  
155 J. Shirley <cpan@coldhardcode.com>
156  
157 =head1 LICENSE
158  
159 This library is free software, you can redistribute it and/or modify
160 it under the same terms as Perl itself.
161  
162 =cut
163  
164  
165 1 1;
« EncodedColumn/Digest.pm ↑ Summary EncodedColumn/Crypt.pm »