| 1 | package Class::Usul::Crypt; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Class::Usul::Constants qw( NUL ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Class::Usul::Functions qw( create_token is_coderef is_hashref ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Crypt::CBC; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Exporter 5.57 qw( import ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 4 | our @EXPORT_OK = qw( cipher_list decrypt default_cipher encrypt ); | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 4 | my $DEFAULT = 'Twofish2'; my $SEED = do { local $RS = undef; <DATA> }; | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | # Private functions | ||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | my $_decode = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 20 | 8 | my $v = $_[ 0 ]; $v =~ tr{ \t}{01}; pack 'b*', $v; | |||||||||||||||||||||||||||||
| 21 | 4 | }; | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 8 | ▶ | my $_prepare = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 24 | 8 | my $v = $_[ 0 ]; my $pad = " \t" x 8; $v =~ s{^$pad|[^ \t]}{}g; $v; | |||||||||||||||||||||||||||||
| 25 | 4 | }; | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 16 | ▶ | my $_dref = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=16.6
| |||||||||||||||||||||||||||||||
| 28 | 16 | ▶ | (is_coderef $_[ 0 ]) ? ($_[ 0 ]->() // NUL) : ($_[ 0 ] // NUL); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$_[0]->() // ""
$_[0] // ""
MC/DC0%0/1
$_[0]->() // ""
$_[0]->()
MC/DC100%1/1
$_[0] // ""
$_[0]
Truth table0%0/2
A
A= $_[0]->()
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||
| 29 | 4 | }; | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 8 | ▶ | my $_eval = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||
| 32 | 8 | ▶ | my $v = $_prepare->( $_[ 0 ] ); $v ? ((eval $_decode->( $v )) || NUL) : NUL; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
eval &$_decode($v) or ""
MC/DC0%0/1
eval &$_decode($v) or ""
eval &$_decode($v)
Truth table50%1/2
A
A= eval &$_decode($v)
| |||||||||||||||||||||||||||||||
| 33 | 4 | }; | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 10 | ▶ | my $_cipher_name = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||
| 36 | 10 | ▶ | (is_hashref $_[ 0 ]) ? $_[ 0 ]->{cipher} || $DEFAULT : $DEFAULT; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0]{"cipher"} or $DEFAULT
MC/DC0%0/2
$_[0]{"cipher"} or $DEFAULT
$_[0]{"cipher"}
$DEFAULT
Truth table66%2/3
A or B
A= $_[0]{"cipher"}B= $DEFAULT
| |||||||||||||||||||||||||||||||
| 37 | 4 | }; | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | 8 | ▶ | my $_compose = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||
| 40 | 8 | ▶ | $_eval->( $_dref->( $_[ 0 ]->{seed} ) || $SEED ).$_dref->( $_[ 0 ]->{salt} ); | ||||||||||||||||||||||||||||
|
Condition66%2/3
&$_dref($_[0]{'seed'}) || $SEED
MC/DC0%0/2
&$_dref($_[0]{'seed'}) || $SEED
&$_dref($_[0]{'seed'})
$SEED
Truth table66%2/3
A || B
A= &$_dref($_[0]{'seed'})B= $SEED
| |||||||||||||||||||||||||||||||
| 41 | 4 | }; | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | 10 | ▶ | my $_new_crypt_cbc = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 44 | 10 | Crypt::CBC->new( -cipher => $_[ 0 ], -key => $_[ 1 ] ); | |||||||||||||||||||||||||||||
| 45 | 4 | }; | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | my $_token = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | create_token( $_compose->( $_[ 0 ] || {} ) ); | ||||||||||||||||||||||||||||
|
Condition100%2/2
$_[0] || {}
MC/DC100%1/1
$_[0] || {}
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||
| 49 | 4 | }; | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | 10 | ▶ | my $_wards = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 52 | 10 | ▶ | (is_hashref $_[ 0 ]) || !$_[ 0 ] ? $_token->( $_[ 0 ] ) : $_[ 0 ]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&is_hashref($_[0]) or not $_[0]
MC/DC100%2/2
&is_hashref($_[0]) or not $_[0]
&is_hashref($_[0])
not $_[0]
Truth table100%3/3
A or not(B)
A= &is_hashref($_[0])B= not $_[0]
| |||||||||||||||||||||||||||||||
| 53 | 4 | }; | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 10 | ▶ | my $_cipher = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 56 | 10 | $_new_crypt_cbc->( $_cipher_name->( $_[ 0 ] ), $_wards->( $_[ 0 ] ) ); | |||||||||||||||||||||||||||||
| 57 | 4 | }; | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||
| 59 | # Public functions | ||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub cipher_list () { | ||||||||||||||||||||||||||||
|
Subroutine
cipher_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 61 | 1 | return ( qw( Blowfish Rijndael Twofish2 ) ); | |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | sub decrypt (;$$) { | ||||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 65 | 5 | return $_cipher->( $_[ 0 ] )->decrypt( decode_base64( $_[ 1 ] ) ); | |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | sub default_cipher () { | ||||||||||||||||||||||||||||
|
Subroutine
default_cipher: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 69 | 2 | return $DEFAULT; | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | sub encrypt (;$$) { | ||||||||||||||||||||||||||||
|
Subroutine
encrypt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 73 | 5 | return encode_base64( $_cipher->( $_[ 0 ] )->encrypt( $_[ 1 ] ), NUL ); | |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | 4 | 1; | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | =head1 Name | ||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | Class::Usul::Crypt - Encryption / decryption functions | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | =head1 Synopsis | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | use Class::Usul::Crypt qw(decrypt encrypt); | ||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | my $args = q(); # OR | ||||||||||||||||||||||||||||||
| 89 | my $args = 'salt'; # OR | ||||||||||||||||||||||||||||||
| 90 | my $args = { salt => 'salt', seed => 'whiten this' }; | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | $args->{cipher} = 'Twofish2'; # Optionally | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | my $base64_encrypted_text = encrypt( $args, $plain_text ); | ||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | my $plain_text = decrypt( $args, $base64_encrypted_text ); | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | =head1 Description | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | Exports a pair of functions to encrypt / decrypt data. Obfuscates the default | ||||||||||||||||||||||||||||||
| 101 | encryption key | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | =head1 Configuration and Environment | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | The C<$key> can be a string (including the null string) or a hash reference | ||||||||||||||||||||||||||||||
| 106 | with I<salt> and I<seed> keys. The I<seed> attribute can be a code reference in | ||||||||||||||||||||||||||||||
| 107 | which case it will be called with no argument and the return value used | ||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | Lifted from L<Acme::Bleach> the default seed for the key generator has been | ||||||||||||||||||||||||||||||
| 110 | whitened and included in this source file | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | The seed is C<eval>'d in string context and then the salt is concatenated onto | ||||||||||||||||||||||||||||||
| 113 | it before being passed to L<create token|Class::Usul::Functions/create_token>. | ||||||||||||||||||||||||||||||
| 114 | Uses this value as the key for a L<Crypt::CBC> object | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | =head1 Subroutines/Methods | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | =head2 decrypt | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | my $plain = decrypt( $salt || \%params, $encoded ); | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | Decodes and decrypts the C<$encoded> argument and returns the plain | ||||||||||||||||||||||||||||||
| 123 | text result. See the L</encrypt> method | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | =head2 encrypt | ||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | my $encoded = encrypt( $salt || \%params, $plain ); | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | Encrypts the plain text passed in the C<$plain> argument and returns | ||||||||||||||||||||||||||||||
| 130 | it Base64 encoded. By default L<Crypt::Twofish2> is used to do the | ||||||||||||||||||||||||||||||
| 131 | encryption. The optional C<< $params->{cipher} >> attribute overrides this | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | =head2 cipher_list | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | @list_of_ciphers = cipher_list(); | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | Returns the list of ciphers supported by L<Crypt::CBC>. These may not | ||||||||||||||||||||||||||||||
| 138 | all be installed | ||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | =head2 default_cipher | ||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | $ciper_name = default_cipher(); | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | Returns I<Twofish2> | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | =head1 Diagnostics | ||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | None | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | =head1 Dependencies | ||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||
| 152 | =over 3 | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | =item L<Crypt::CBC> | ||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||
| 156 | =item L<Crypt::Twofish2> | ||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | =item L<Exporter> | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | =item L<MIME::Base64> | ||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | =back | ||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | =head1 Incompatibilities | ||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | There are no known incompatibilities in this module | ||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||
| 168 | =head1 Bugs and Limitations | ||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | There are no known bugs in this module. | ||||||||||||||||||||||||||||||
| 171 | Please report problems to the address below. | ||||||||||||||||||||||||||||||
| 172 | Patches are welcome | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | =head1 Author | ||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | Peter Flanigan, C<< <pjfl@cpan.org> >> | ||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||
| 178 | =head1 License and Copyright | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | Copyright (c) 2018 Peter Flanigan. All rights reserved | ||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||
| 182 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||
| 183 | under the same terms as Perl itself. See L<perlartistic> | ||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||
| 185 | This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||
| 186 | but WITHOUT WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||
| 187 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | # Local Variables: | ||||||||||||||||||||||||||||||
| 192 | # mode: perl | ||||||||||||||||||||||||||||||
| 193 | # tab-width: 3 | ||||||||||||||||||||||||||||||
| 194 | # End: | ||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||
| 196 | __DATA__ | ||||||||||||||||||||||||||||||