blib/lib/DTOne/Crypt.pm

filter: Statementstmt 94.8% 55 / 58 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 89.4% 76 / 85 SCAR 19.6
CC
7
Cov
87%
encrypt_aes256gcm
4.3
decrypt_aes256gcm
4.3
BEGIN
1.0
CRAP 7.1
↑ 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.04';
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 1 ▶ my $plaintext = shift;
Subroutine encrypt_aes256gcm: called CC=4 SCAR=14.7
Pod documented
24 1 ▶ my $master_key = shift or croak "master key required";
Branch: 1 branch
BranchTrueFalseTotal
my $master_key = shift() or croak("master key required") 0 1 1
25  
26 1 ▶ unless (defined $plaintext) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $plaintext) 0 1 1
27 0 croak "plaintext data required";
28 }
29  
30 1 $master_key = decode_base64($master_key);
31 1 ▶ unless (length($master_key) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $master_key == 32) 0 1 1
32 0 croak "invalid master key length";
33 }
34  
35 1 my $iv = random_bytes(12);
36 1 my $salt = random_bytes(16);
37 1 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 1 my ($ciphertext, $tag) = gcm_encrypt_authenticate(
47 'AES',
48 $key,
49 $iv,
50 undef,
51 $plaintext
52 );
53  
54 1 return encode_base64(join('', $salt, $iv, $tag, $ciphertext), '');
55 }
56  
57 sub decrypt_aes256gcm {
58 1 ▶ my $encrypted = shift or croak "encrypted data required";
Subroutine decrypt_aes256gcm: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $encrypted = shift() or croak("encrypted data required") 0 1 1
59 1 ▶ my $master_key = shift or croak "master key required";
Branch: 1 branch
BranchTrueFalseTotal
my $master_key = shift() or croak("master key required") 0 1 1
60  
61 1 $master_key = decode_base64($master_key);
62 1 ▶ unless (length($master_key) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $master_key == 32) 0 1 1
63 0 croak "invalid master key length";
64 }
65  
66 1 $encrypted = decode_base64($encrypted);
67 1 my ($salt, $iv, $tag, $ciphertext) = unpack('a16 a12 a16 a*', $encrypted);
68 1 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 1 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 »