blib/lib/DTOne/Crypt.pm

filter: Statementstmt 100% 58 / 58 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 100% 85 / 85 SCAR 19.5
CC
7
Cov
100%
encrypt_aes256gcm
4.0
decrypt_aes256gcm
4.0
BEGIN
1.0
CRAP 7.0
↑ Summary Build »
1 package DTOne::Crypt;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.05';
6  
7 1 require Exporter;
8 1 our @ISA = qw(Exporter);
9 1 our @EXPORT_OK = qw(encrypt_aes256gcm decrypt_aes256gcm);
10  
11 1 ▶use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Crypt::ScryptKDF qw(scrypt_raw);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Bytes::Random::Secure qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use constant SCRYPT_ITERATIONS => 32768; # 2**15
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant SCRYPT_BLOCK_SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant SCRYPT_PARALLELISM => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant SCRYPT_DERIVED_KEY_LEN => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 sub encrypt_aes256gcm {
23 8 ▶ my $plaintext = shift;
Subroutine encrypt_aes256gcm: called CC=4 SCAR=13.9
Pod documented
24 8 ▶ my $master_key = shift or croak "master key required";
Branch: 1 branch
BranchTrueFalseTotal
my $master_key = shift() or croak("master key required") 2 6 8
25  
26 6 ▶ unless (defined $plaintext) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $plaintext) 1 5 6
27 1 croak "plaintext data required";
28 }
29  
30 5 $master_key = decode_base64($master_key);
31 5 ▶ unless (length($master_key) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $master_key == 32) 2 3 5
32 2 croak "invalid master key length";
33 }
34  
35 3 my $iv = random_bytes(12);
36 3 my $salt = random_bytes(16);
37 3 my $key = scrypt_raw(
38 $master_key,
39 $salt,
40 SCRYPT_ITERATIONS,
41 SCRYPT_BLOCK_SIZE,
42 SCRYPT_PARALLELISM,
43 SCRYPT_DERIVED_KEY_LEN
44 );
45  
46 3 my ($ciphertext, $tag) = gcm_encrypt_authenticate(
47 'AES',
48 $key,
49 $iv,
50 undef,
51 $plaintext
52 );
53  
54 3 return encode_base64(join('', $salt, $iv, $tag, $ciphertext), '');
55 }
56  
57 sub decrypt_aes256gcm {
58 9 ▶ my $encrypted = shift or croak "encrypted data required";
Subroutine decrypt_aes256gcm: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $encrypted = shift() or croak("encrypted data required") 3 6 9
59 6 ▶ my $master_key = shift or croak "master key required";
Branch: 1 branch
BranchTrueFalseTotal
my $master_key = shift() or croak("master key required") 2 4 6
60  
61 4 $master_key = decode_base64($master_key);
62 4 ▶ unless (length($master_key) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $master_key == 32) 2 2 4
63 2 croak "invalid master key length";
64 }
65  
66 2 $encrypted = decode_base64($encrypted);
67 2 my ($salt, $iv, $tag, $ciphertext) = unpack('a16 a12 a16 a*', $encrypted);
68 2 my $key = scrypt_raw(
69 $master_key,
70 $salt,
71 SCRYPT_ITERATIONS,
72 SCRYPT_BLOCK_SIZE,
73 SCRYPT_PARALLELISM,
74 SCRYPT_DERIVED_KEY_LEN
75 );
76  
77 2 return gcm_decrypt_verify(
78 'AES',
79 $key,
80 $iv,
81 undef,
82 $ciphertext,
83 $tag
84 );
85 }
86  
87 1 1;
88 __END__
↑ Summary Build »