| 1 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Copyright 1999, Peter Marelas. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # This library is free software; you can redistribute it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Bug reports and comments to maral@phase-one.com.au. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | package CGI::EncryptForm; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Crypt::HCE_SHA; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Storable qw(freeze thaw); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Digest::SHA1 qw(sha1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use vars qw($VERSION $CHARSET); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | $VERSION = 1.02; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # Default character set we use for encoding/decoding encrypted string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | $CHARSET = [qw/AA Aa BA Ba CA Ca DA Da EA Ea AB Ab BB Bb CB Cb DB Db EB Eb AC Ac BC Bc CC Cc DC Dc EC Ec AD Ad BD Bd CD Cd DD Dd ED Ed AE Ae BE Be CE Ce DE De EE Ee AF Af BF Bf CF Cf DF Df EF Ef AG Ag BG Bg CG Cg DG Dg EG Eg AH Ah BH Bh CH Ch DH Dh EH Eh AI Ai BI Bi CI Ci DI Di EI Ei AJ Aj BJ Bj CJ Cj DJ Dj EJ Ej AK Ak BK Bk CK Ck DK Dk EK Ek AL Al BL Bl CL Cl DL Dl EL El AM Am BM Bm CM Cm DM Dm EM Em AN An BN Bn CN Cn DN Dn EN En AO Ao BO Bo CO Co DO Do EO Eo AP Ap BP Bp CP Cp DP Dp EP Ep AQ Aq BQ Bq CQ Cq DQ Dq EQ Eq AR Ar BR Br CR Cr DR Dr ER Er AS As BS Bs CS Cs DS Ds ES Es AT At BT Bt CT Ct DT Dt ET Et AU Au BU Bu CU Cu DU Du EU Eu AV Av BV Bv CV Cv DV Dv EV Ev AW Aw BW Bw CW Cw DW Dw EW Ew AX Ax BX Bx CX Cx DX Dx EX Ex AY Ay BY By CY Cy DY Dy EY Ey yY wW vV uU zU zZ/]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | my($this) = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | $self->_initialize(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | return($self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # Public Method: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # encrypt() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # Encrypt the hash reference | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # Constructs: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # encrypt() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Return last encrypted string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # encrypt({a => b}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Encrypt hash reference and return encrypted string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | sub encrypt { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encrypt: called CC=7 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | my($self, $decrypted_hashref) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | my $secret_key = $self->secret_key() || return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->secret_key || (return undef)
MC/DC0%0/1
$self->secret_key || (return undef)
$self->secret_key
Truth table50%1/2
A
A= $self->secret_key
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if (!defined($decrypted_hashref) && !defined($self->{_encrypted_string})) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $decrypted_hashref || defined $self->{'_encrypted_string'}
MC/DC0%0/2
defined $decrypted_hashref || defined $self->{'_encrypted_string'}
defined $decrypted_hashref
defined $self->{'_encrypted_string'}
Truth table33%1/3
A || B
A= defined $decrypted_hashrefB= defined $self->{'_encrypted_string'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $self->error('encrypt() This is the first time encrypt() has been called and therefore requires an arguement.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | elsif (!defined($decrypted_hashref)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return($self->{_encrypted_string}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if (ref($decrypted_hashref) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->error('encrypt() This method accepts a single hash reference only.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | my $random_key = $self->_random_key(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | my $str = freeze($decrypted_hashref); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | $str = sha1($str) . $str; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | my $cipher = Crypt::HCE_SHA->new($secret_key, $random_key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | $self->{_encrypted_string} = $self->usecharset() ? | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | $self->_encode($random_key . $cipher->hce_block_encrypt($str)) : $random_key . $cipher->hce_block_encrypt($str); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | $self->error(''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | return($self->{_encrypted_string}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # Public Method: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # decrypt() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Decryption routine | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # Constructs: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # decrypt() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Return last decrypted reference to hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # decrypt("encrypted string") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # Decrypt encrypted string and return reference to hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub decrypt { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=11 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | my($self, $encrypted_string) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 6 | ▶ | my $secret_key = $self->secret_key() || return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->secret_key || (return undef)
MC/DC0%0/1
$self->secret_key || (return undef)
$self->secret_key
Truth table50%1/2
A
A= $self->secret_key
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 6 | my $random_key; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 6 | ▶ | if (!defined($encrypted_string) && !defined($self->{_decrypted_hashref})) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $encrypted_string || defined $self->{'_decrypted_hashref'}
MC/DC0%0/2
defined $encrypted_string || defined $self->{'_decrypted_hashref'}
defined $encrypted_string
defined $self->{'_decrypted_hashref'}
Truth table33%1/3
A || B
A= defined $encrypted_stringB= defined $self->{'_decrypted_hashref'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $self->error('decrypt() This is the first time decrypt() has been called and therefore requires an arguement.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | elsif (!defined($encrypted_string)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | return($self->{_decrypted_hashref}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # if using char set ensure string is even number | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | ▶ | if ($self->usecharset() && (length($encrypted_string) % 2) != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->usecharset and length($encrypted_string) % 2 != 0
MC/DC50%1/2
$self->usecharset and length($encrypted_string) % 2 != 0
$self->usecharset
length($encrypted_string) % 2 != 0
Truth table66%2/3
A and B
A= $self->usecharsetB= length($encrypted_string) % 2 != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | $self->error('decrypt() Character set is inconsistent.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # decode the encrypted string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 5 | ▶ | my $str = $self->usecharset() ? $self->_decode($encrypted_string) : | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | $encrypted_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # extract the random key (first 4 bytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | $random_key = substr($str, 0, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 5 | ▶ | if (length($random_key) != 4) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | $self->error('decrypt() Random key is inconsistent.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | $str = substr($str, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # decrypt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 4 | my $cipher = Crypt::HCE_SHA->new($secret_key, $random_key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 4 | my $plaintxt = $cipher->hce_block_decrypt($str); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # extract sha1 digest from decrypted string which is always | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # 20 bytes long | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | my $digest = substr($plaintxt, 0, 20); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | if (length($digest) != 20) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $self->error('decrypt() Digest is inconsistent.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 3 | $plaintxt = substr($plaintxt, 20); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # check stored decrypted digest against digest of decrypted string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | if ($digest ne sha1($plaintxt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | $self->error('decrypt() Encrypted string is inconsistent.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | $self->error(''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | return(thaw($plaintxt)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # Public Method: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # secret_key() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # Set/Return secret key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Constructs: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # secret_key() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # Return current secret_key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # secret_key("secret key") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # Set secret key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 12 | ▶ | sub secret_key { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
secret_key: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 12 | my($self, $secret_key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | ▶ | if (defined($secret_key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | $self->{_secret_key} = $secret_key; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | elsif (!defined($self->{_secret_key})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $self->error('secret_key() No secret key has been defined.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 12 | $self->error(''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 12 | return($self->{_secret_key}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Public Method | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # charset() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # Set character set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # Constructors: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # charset([array of 2 character length elements from 0 to 255]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # Set character set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 2 | ▶ | sub charset { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
charset: called CC=6 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | my($self, $charset) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | ▶ | if ((defined($charset) && ref($charset) ne 'ARRAY') || !defined($charset)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
defined $charset and ref $charset ne "ARRAY" or not defined $charset
defined $charset and ref $charset ne "ARRAY"
MC/DC0%0/3
defined $charset and ref $charset ne "ARRAY" or not defined $charset
defined $charset
ref $charset ne "ARRAY"
not defined $charset
Truth table20%1/5
A and B or not(C)
A= defined $charsetB= ref $charset ne "ARRAY"C= not defined $charset
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | $self->error('charset() This methods accepts a single array reference.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ▶ | elsif (defined($charset) && $#$charset != 255) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $charset and $charset->$#* != 255
MC/DC0%0/2
defined $charset and $charset->$#* != 255
defined $charset
$charset->$#* != 255
Truth table33%1/3
A and B
A= defined $charsetB= $charset->$#* != 255
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $self->error('charset() The character set is invalid.'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | $self->{'_charset'} = $charset; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | $self->{'_charset_hash'} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | map { $self->{'_charset'}->[$_] => $_; } 0..$#{$self->{'_charset'}} | |||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | $self->error(''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # Public Method: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # usecharset() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # Enable/Return character set encoding/decoding of encrypted/decrypted string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # suitable for storage in form fields, cookies or URL's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # Constructs: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # usecharset() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # Return current usecharset value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # usecharset(0 or 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # Set charset encoding/decoding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 14 | ▶ | sub usecharset { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usecharset: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 14 | my($self, $usecharset) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 14 | $self->error(''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 14 | ▶ | $self->{_usecharset} = $usecharset if defined($usecharset); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 14 | ▶ | return($self->{_usecharset} ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # Public/Private Method: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # error() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # Set/Clear/Get error message for last operation on object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # Public Constructors: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # error() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # Return error from last operation | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 40 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 40 | my($self, $errormsg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 40 | ▶ | if ($errormsg) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 4 | $self->{_errormsg} = "Error: $errormsg\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 36 | return($self->{_errormsg}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # Private methods - dont call these from your object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | ▶ | sub _initialize { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=3 SCAR=12.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | my($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | $self->{'_usecharset'} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | $self->{'_encrypted_string'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | $self->{'_decrypted_hashref'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | $self->{'_secret_key'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 1 | $self->{'_random_key'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | $self->{'_errormsg'} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 1 | $self->{'_charset'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 1 | $self->{'_charset_hash'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # Set default charset | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | $self->charset($CHARSET); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | foreach (keys(%opts)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if (! $self->can($_)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $self->error("_initialize() $_ is not a valid option."); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | $self->$_($opts{$_}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # Autogenerate the current object's random key every time. i.e. Not Persistent! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 3 | ▶ | sub _random_key { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_random_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | my($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | $self->{_random_key} = pack("CCCC", rand(255), rand(255), rand(255), | |||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | rand(255)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | $self->error(''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 3 | return($self->{_random_key}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # Decode encrypted string using character set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 5 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 5 | my($self, $decode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 5 | $decode =~ s/(.{2})/chr($self->{'_charset_hash'}->{$1})/sge; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 5 | return($decode); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # Encode encrypted string using character set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | my($self, $encode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 3 | $encode =~ s/(.)/$self->{'_charset'}->[ord($1)]/sge; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 3 | return($encode); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||