File Coverage

blib/lib/Argon/Encryption.pm
Criterion Covered Total %
statement 29 32 90.6
branch 0 2 0.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 0 1 0.0
total 41 49 83.6


line stmt bran cond sub pod time code
1             package Argon::Encryption;
2             # ABSTRACT: Role providing methods and attributes to encrypt Argon::Message traffic
3             $Argon::Encryption::VERSION = '0.17';
4              
5 4     4   500194 use strict;
  4         18  
  4         129  
6 4     4   26 use warnings;
  4         9  
  4         125  
7 4     4   22 use Carp;
  4         7  
  4         282  
8 4     4   1314 use Moose::Role;
  4         21647  
  4         23  
9 4     4   38644 use Moose::Util::TypeConstraints;
  4         14  
  4         26  
10 4     4   14667 use Crypt::CBC;
  4         29295  
  4         285  
11 4     4   526 use Path::Tiny qw(path);
  4         7756  
  4         320  
12 4     4   351 use Argon::Types;
  4         13  
  4         1431  
13              
14             my %CIPHER;
15              
16              
17             has keyfile => (
18             is => 'ro',
19             isa => 'Ar::FilePath',
20             );
21              
22              
23             has key => (
24             is => 'ro',
25             isa => 'Str',
26             lazy => 1,
27             builder => '_build_key',
28             );
29              
30             sub _build_key {
31 0     0   0 my $self = shift;
32 0 0       0 croak 'keyfile required if key is not specified'
33             unless $self->keyfile;
34 0         0 path($self->keyfile)->slurp_raw;
35             }
36              
37             has cipher => (
38             is => 'ro',
39             isa => 'Crypt::CBC',
40             lazy => 1,
41             builder => '_build_cipher',
42             handles => {
43             encrypt => 'encrypt_hex',
44             decrypt => 'decrypt_hex',
45             },
46             );
47              
48             sub _build_cipher {
49 11     11   29 my $self = shift;
50              
51 11   66     407 $CIPHER{$self->key} ||= Crypt::CBC->new(
52             -key => $self->key,
53             -cipher => 'Rijndael',
54             -salt => 1,
55             );
56              
57 11         3713 $CIPHER{$self->key};
58             }
59              
60              
61             has token => (
62             is => 'ro',
63             isa => 'Str',
64             lazy => 1,
65             builder => 'create_token',
66             );
67              
68             sub create_token {
69 8     8 0 21 my $self = shift;
70 8         284 unpack 'H*', $self->cipher->random_bytes(8);
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Argon::Encryption - Role providing methods and attributes to encrypt Argon::Message traffic
84              
85             =head1 VERSION
86              
87             version 0.17
88              
89             =head1 DESCRIPTION
90              
91             Role that provides for encrypting messages in the Argon system. Notably
92             provides the C<keyfile>, C<key>, and C<token> attributes.
93              
94             =head1 ATTRIBUTES
95              
96             =head2 keyfile
97              
98             The path to a file containing the encryption pass phrase. Either L</key> or
99             C<keyfile> must be provided when instantiating a class implementing
100             C<Argon::Encryption>.
101              
102             =head2 key
103              
104             The encryption pass phrase.
105              
106             =head2 token
107              
108             A string of random bytes used as a channel or service identifier.
109              
110             =head1 AUTHOR
111              
112             Jeff Ober <sysread@fastmail.fm>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2017 by Jeff Ober.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut