| 1 | package Secret::Simple; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use vars qw( @ISA @EXPORT ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Crypt::CBC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 15 | 3 | @EXPORT = qw( ssdecrypt ssdecryptraw ssencrypt ssencryptraw ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | my $DEFAULT_CIPHER = 'Rijndael_PP'; | |||||||||||||||||||||||||||
| 18 | 3 | my $DEFAULT_KEYFILE = '~/.ssh/id_dsa'; | |||||||||||||||||||||||||||
| 19 | 3 | my $DEFAULT_GARBAGE = 'eLH6eDl7H+Ng07Zj'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 3 | my ($class, @args) = @_; | |||||||||||||||||||||||||||
| 23 | 3 | my $self = {}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | my (%args, %option); | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | @args = ( 'key', '{sskeyfile}' ) unless @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | if (ref($args[0]) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | %option = %{$args[0]}; | |||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||
| 30 | 3 | ▶ | @args = ( 'key', $args[0] ) if @args == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | croak "Invalid arguments passed" if scalar(@args) & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 3 | %args = @args; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 3 | my %tmp = map { $_ => 1 } qw( key keyfilesize ); | |||||||||||||||||||||||||||
| 35 | 3 | for my $opt (keys %args) { | |||||||||||||||||||||||||||
| 36 | 3 | my $opt2 = $opt; | |||||||||||||||||||||||||||
| 37 | 3 | $opt2 =~ s/^-//; | |||||||||||||||||||||||||||
| 38 | 3 | ▶ | croak "Unrecognized -$opt2 option passed" unless $tmp{$opt2}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 3 | $option{$opt2} = $args{$opt}; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 3 | ▶ | $option{key} = '{sskeyfile}' unless $option{key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 3 | key($self, $option{key}); | |||||||||||||||||||||||||||
| 43 | 3 | $self->{keyfilesize} = 0; | |||||||||||||||||||||||||||
| 44 | 3 | ▶ | keyfilesize($self, $option{keyfilesize}) if $option{keyfilesize}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 3 | $self->{keydata} = keydata($self); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | bless($self, $class); | |||||||||||||||||||||||||||
| 48 | 3 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub decrypt { | ||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 1 | my ($self, $b64ciphertext) = @_; | |||||||||||||||||||||||||||
| 53 | 1 | ▶ | return unless $b64ciphertext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | my $ciphertext = decode_base64($b64ciphertext); | |||||||||||||||||||||||||||
| 55 | 1 | my $plaintext = decryptraw($self, $ciphertext); | |||||||||||||||||||||||||||
| 56 | 1 | return $plaintext; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub decryptraw { | ||||||||||||||||||||||||||
|
Subroutine
decryptraw: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 2 | my ($self, $ciphertext) = @_; | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | return unless $ciphertext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | my $cipher = Crypt::CBC->new( | ||||||||||||||||||||||||||||
| 63 | -key => $self->{keydata}, | ||||||||||||||||||||||||||||
| 64 | 2 | -cipher => $DEFAULT_CIPHER, | |||||||||||||||||||||||||||
| 65 | -header => 'none', | ||||||||||||||||||||||||||||
| 66 | -iv => $DEFAULT_GARBAGE | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | 2 | my $plaintext = $cipher->decrypt($ciphertext); | |||||||||||||||||||||||||||
| 69 | 2 | return $plaintext; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub encrypt { | ||||||||||||||||||||||||||
|
Subroutine
encrypt: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1 | my ($self, $plaintext) = @_; | |||||||||||||||||||||||||||
| 74 | 1 | my $ciphertext = encryptraw($self, $plaintext); | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | return unless $ciphertext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | return encode_base64( $ciphertext ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub encryptraw { | ||||||||||||||||||||||||||
|
Subroutine
encryptraw: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 2 | my ($self, $plaintext) = @_; | |||||||||||||||||||||||||||
| 81 | 2 | ▶ | return unless $plaintext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | my $cipher = Crypt::CBC->new( | ||||||||||||||||||||||||||||
| 83 | -key => $self->{keydata}, | ||||||||||||||||||||||||||||
| 84 | 2 | -cipher => $DEFAULT_CIPHER, | |||||||||||||||||||||||||||
| 85 | -header => 'none', | ||||||||||||||||||||||||||||
| 86 | -iv => $DEFAULT_GARBAGE | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | 2 | my $ciphertext = $cipher->encrypt($plaintext); | |||||||||||||||||||||||||||
| 89 | 2 | return $ciphertext; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | sub key { | ||||||||||||||||||||||||||
|
Subroutine
key: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 3 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 94 | 3 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 3 | ▶ | croak "Bad key specification" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $key and ref $key ne "ARRAY"
MC/DC0%0/2
ref $key and ref $key ne "ARRAY"
ref $key
ref $key ne "ARRAY"
Truth table66%2/3
A and B
A= ref $keyB= ref $key ne "ARRAY"
| |||||||||||||||||||||||||||||
| 96 | if ref($key) && ref($key) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 97 | 3 | $self->{key} = $key; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 3 | return $self->{key}; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | ▶ | sub keydata { | ||||||||||||||||||||||||||
|
Subroutine
keydata: called CC=7 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | unless (defined $self->{keydata}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # calculate aggregate key data | ||||||||||||||||||||||||||||
| 107 | my @keys = ref($self->{key}) eq 'ARRAY' ? | ||||||||||||||||||||||||||||
| 108 | 3 | ▶ | @{$self->{key}} : ( $self->{key} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 3 | my $data = ""; | |||||||||||||||||||||||||||
| 110 | 3 | for my $frag (@keys) { | |||||||||||||||||||||||||||
| 111 | 3 | my $piece = $frag; | |||||||||||||||||||||||||||
| 112 | 3 | ▶ | if ($frag =~ /^\{sskeyfile\}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | my $fn = $frag; | |||||||||||||||||||||||||||
| 114 | 0 | $fn =~ s/^\{sskeyfile\}//; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | $fn = $DEFAULT_KEYFILE unless $fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($fn1) = glob($fn); | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | croak "No access to specified key file '$fn'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | unless -r $fn1; | ||||||||||||||||||||||||||||
| 119 | 0 | $piece = _read_rawfile($fn1, $self->{keyfilesize}); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 3 | $data .= $piece; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 3 | $self->{keydata} = $data; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | return $self->{keydata}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub keyfilesize { | ||||||||||||||||||||||||||
|
Subroutine
keyfilesize: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($self, $num) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | croak "Bad limit passed" if defined $num && $num !~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $num and not $num =~ /^\d+$/
MC/DC0%0/2
defined $num and not $num =~ /^\d+$/
defined $num
not $num =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= defined $numB= not $num =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | $self->{keyfilesize} = $num if defined $num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | return $self->{keyfilesize}; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # The procedural style function section begins here. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | sub ssdecrypt { | ||||||||||||||||||||||||||
|
Subroutine
ssdecrypt: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 1 | my ($b64ciphertext, @keyspec) = @_; | |||||||||||||||||||||||||||
| 140 | 1 | ▶ | return unless $b64ciphertext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 1 | my $ciphertext = decode_base64($b64ciphertext); | |||||||||||||||||||||||||||
| 142 | 1 | my $plaintext = ssdecryptraw($ciphertext, @keyspec); | |||||||||||||||||||||||||||
| 143 | 1 | return $plaintext; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | ▶ | sub ssdecryptraw { | ||||||||||||||||||||||||||
|
Subroutine
ssdecryptraw: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 1 | my ($ciphertext, @keyspec) = @_; | |||||||||||||||||||||||||||
| 148 | 1 | ▶ | return unless $ciphertext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | my $ss = @keyspec ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | Secret::Simple->new( key => [ @keyspec ] ) : | ||||||||||||||||||||||||||||
| 151 | Secret::Simple->new(); | ||||||||||||||||||||||||||||
| 152 | 1 | my $plaintext = $ss->decryptraw($ciphertext); | |||||||||||||||||||||||||||
| 153 | 1 | return $plaintext; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | sub ssencrypt { | ||||||||||||||||||||||||||
|
Subroutine
ssencrypt: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 1 | my ($plaintext, @keyspec) = @_; | |||||||||||||||||||||||||||
| 158 | 1 | ▶ | return unless $plaintext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | my $ciphertext = ssencryptraw($plaintext, @keyspec); | |||||||||||||||||||||||||||
| 160 | 1 | ▶ | return unless $ciphertext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 1 | return encode_base64( $ciphertext ); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | sub ssencryptraw { | ||||||||||||||||||||||||||
|
Subroutine
ssencryptraw: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 1 | my ($plaintext, @keyspec) = @_; | |||||||||||||||||||||||||||
| 166 | 1 | ▶ | return unless $plaintext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 1 | ▶ | my $ss = @keyspec ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | Secret::Simple->new( key => [ @keyspec ] ) : | ||||||||||||||||||||||||||||
| 169 | Secret::Simple->new(); | ||||||||||||||||||||||||||||
| 170 | 1 | my $ciphertext = $ss->encryptraw($plaintext); | |||||||||||||||||||||||||||
| 171 | 1 | return $ciphertext; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # The private module function section begins here. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # The _read_rawfile private function accepts a filename and an optional | ||||||||||||||||||||||||||||
| 177 | # limit argument. The entire contents of a specified file will be read | ||||||||||||||||||||||||||||
| 178 | # and returned as a string if the limit is undefined or zero, but a | ||||||||||||||||||||||||||||
| 179 | # maximum of $limit bytes will be read in and returned otherwise. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _read_rawfile { | ||||||||||||||||||||||||||
|
Subroutine
_read_rawfile: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 182 | 0 | my ($fn, $limit) = @_; | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | croak "No filename argument passed" unless $fn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | croak "Bad limit passed" if $limit && $limit !~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$limit and not $limit =~ /^\d+$/
MC/DC0%0/2
$limit and not $limit =~ /^\d+$/
$limit
not $limit =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= $limitB= not $limit =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 185 | 0 | my ($chunk, $num, $data, $buf) = ( 8192, 0, "" ); | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | croak "Unable to read from file" unless | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | open my ($F), $fn; | ||||||||||||||||||||||||||||
| 188 | 0 | binmode($F); | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | until ( eof($F) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | $chunk = $limit - $num if $limit && $num + $chunk > $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$limit and $num + $chunk > $limit
MC/DC0%0/2
$limit and $num + $chunk > $limit
$limit
$num + $chunk > $limit
Truth table0%0/3
A and B
A= $limitB= $num + $chunk > $limit
| |||||||||||||||||||||||||||||
| 191 | 0 | $num += read($F, $buf, $chunk); | |||||||||||||||||||||||||||
| 192 | 0 | $data .= $buf; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | last if $limit && $num >= $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$limit and $num >= $limit
MC/DC0%0/2
$limit and $num >= $limit
$limit
$num >= $limit
Truth table0%0/3
A and B
A= $limitB= $num >= $limit
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 0 | close $F; | |||||||||||||||||||||||||||
| 196 | 0 | return $data; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 3 | 1; | |||||||||||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||||||||||||