blib/lib/DBIx/Class/FilterColumn/Encrypt.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 66.6% 4 / 6 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 85.9% 55 / 64 CC 6 SCAR 8.9
CC
6
Cov
88%
register_column
4.1
__ANON__
3.1
BEGIN
1.0
CRAP 2.4
↑ Summary
1 package DBIx::Class::FilterColumn::Encrypt;
2 2 $DBIx::Class::FilterColumn::Encrypt::VERSION = '0.003';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use parent 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 __PACKAGE__->load_components(qw/FilterColumn/);
8  
9 2 ▶use Crypt::AuthEnc::GCM 0.048;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Crypt::SysRandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 my $format = 'w a16 a16 a*';
13  
14 2 ▶sub register_column {
Subroutine register_column: called CC=4 SCAR=14.1
Pod documented
15 2 my ($self, $column, $info, @rest) = @_;
16  
17 2 $self->next::method($column, $info, @rest);
18 2 ▶ return unless my $encrypt = $info->{encrypt};
Branch: 1 branch
BranchTrueFalseTotal
unless my $encrypt = $info->{"encrypt"} 1 1 2
19 1 my %keys = %{ $encrypt->{keys} };
20 0 ▶ my $active = $encrypt->{active_key} || (sort { $b <=> $a } keys %keys)[0];
Condition33%1/3
$encrypt->{'active_key'} || (sort {$b <=> $a;} keys %keys)[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
$encrypt->{'active_key'} || (sort {$b <=> $a;} keys %keys)[0]
$encrypt->{'active_key'} (sort {$b <=> $a;} keys %keys)[0]
Truth table33%1/3
A || B
A= $encrypt->{'active_key'}B= (sort {$b <=> $a;} keys %keys)[0]
ABresult
1X1
011
000
21 1 ▶ my $cipher = $encrypt->{cipher} || 'AES';
Condition50%1/2
$encrypt->{'cipher'} || 'AES'
l!l
01
MC/DC0%0/1
$encrypt->{'cipher'} || 'AES'
$encrypt->{'cipher'}
Truth table50%1/2
A
A= $encrypt->{'cipher'}
Aresult
11
00
22  
23 $self->filter_column(
24 $column => {
25 1 ▶ filter_to_storage => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 my (undef, $plaintext) = @_;
27 1 my $iv = Crypt::SysRandom::random_bytes(16);
28 1 my $encrypter = Crypt::AuthEnc::GCM->new($cipher, $keys{$active}, $iv);
29 1 my $ciphertext = $encrypter->encrypt_add($plaintext);
30 1 my $tag = $encrypter->encrypt_done;
31 1 return pack $format, $active, $iv, $tag, $ciphertext;
32 },
33 1 ▶ filter_from_storage => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
34 1 my (undef, $raw) = @_;
35 1 my ($key_id, $iv, $expected_tag, $ciphertext) = unpack $format, $raw;
36 1 ▶ my $key = $keys{$key_id} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $keys{$key_id} or return undef 1 0 1
37 1 my $decrypter = Crypt::AuthEnc::GCM->new($cipher, $key, $iv);
38 1 my $plaintext = $decrypter->decrypt_add($ciphertext);
39 1 my $received_tag = $decrypter->decrypt_done;
40 1 ▶ return $received_tag eq $expected_tag ? $plaintext : undef;
Branch: 1 branch
BranchTrueFalseTotal
$received_tag eq $expected_tag ? : 1 0 1
41 },
42 },
43 1 );
44 }
45  
46 2 1;
47  
48 # ABSTRACT: Transparently encrypt columns in DBIx::Class
49  
50 __END__
↑ Summary