lib/Tie/EncryptedHash.pm

filter: Statementstmt 90.4% 132 / 146 Branchbran 86.3% 76 / 88 Conditioncond 67.2% 37 / 55 MC/DCmcdc 47.2% 17 / 36 Subroutinesub 76.0% 19 / 25 Podpod 0.0% 0 / 9 totaltotal 78.2% 281 / 359 CC 65 SCAR 32.8
CC
65
Cov
85%
_access
50.7
verify
6.2
debug
6.0
CRAP 26.6
↑ Summary
1 #!/usr/bin/perl -s
2 ##
3 ## Tie::EncryptedHash - A tied hash with encrypted fields.
4 ##
5 ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved.
6 ## This code is based on Damian Conway's Tie::SecureHash.
7 ##
8 ## $Id: EncryptedHash.pm,v 1.8 2000/09/02 19:23:00 vipul Exp vipul $
9 ## vi:expandtab=1;ts=4;
10  
11 package Tie::EncryptedHash;
12
13 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use vars qw($VERSION $strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 ( $VERSION ) = '$Revision: 1.8 $' =~ /\s(\d+\.\d+)\s/;
21  
22 7 my $DEBUG = 0;
23  
24 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
25 0 ▶ return undef unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $DEBUG 0 0 0
26 0 my ($caller, undef) = caller;
27 0 my (undef,undef,$line,$sub) = caller(1); $sub =~ s/.*://;
28 0 $sub = sprintf "%10s()%4d",$sub,$line;
29 0 print "$sub " . (shift) . "\n";
30 }
31  
32 # sub new {
33 # my ($class,%args) = @_;
34 # my %self = (); tie %self, $class;
35 # my $self = bless \%self, $class;
36 # $self->{__password} = $args{__password} if $args{__password};
37 # $self->{__cipher} = $args{__cipher} || qq{Blowfish};
38 # return $self;
39 # }
40  
41 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
42 3 my ($class,%args) = @_;
43 3 my $self = {}; tie %$self, $class;
44 3 bless $self, $class;
45 3 ▶ $self->{__password} = $args{__password} if $args{__password};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"__password"} 1 2 3
46 3 ▶ $self->{__cipher} = $args{__cipher} || qq{Blowfish};
Condition100%2/2
$args{'__cipher'} || 'Blowfish'
l!l
12
MC/DC100%1/1
$args{'__cipher'} || 'Blowfish'
$args{'__cipher'}
Truth table100%2/2
A
A= $args{'__cipher'}
Aresult
11
00
47 3 return $self;
48 }
49  
50  
51 741 ▶sub _access {
Subroutine _access: called CC=44 SCAR=39.3
52  
53 741 my ($self, $key, $caller, $file, $value, $delete) = @_;
54 741 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
74100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
55 # SPECIAL ATTRIBUTE
56 741 ▶ if ( $key =~ /(__password|__hide|__scaffolding|__cipher)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($key =~ /(__password|__hide|__scaffolding|__cipher)$/) { } 104 637 741
elsif ($key =~ /^(_{1}[^_][^:]*)$/ or $key =~ /.*?::(_{1}[^_][^:]*)/) { } 573 64 637
elsif ($key =~ /([^:]*)$/ or $key =~ /.*?::([^:]*)/) { } 64 0 64
57 104 my $key = $1;
58 104 ▶ unless($value||$delete) {return undef unless $caller eq $class}
Branch: 2 branches
BranchTrueFalseTotal
unless ($value or $delete) 26 78 104
unless $caller eq $class 26 0 26
Condition100%3/3
$value or $delete
l!l&&r!l&&!r
73526
MC/DC100%2/2
$value or $delete
$value $delete
Truth table100%3/3
A or B
A= $valueB= $delete
ABresult
1X1
011
000
59 78 ▶ if ($delete && ($key =~ /__password/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete and $key =~ /__password/) 4 74 78
Condition100%3/3
$delete and $key =~ /__password/
!ll&&!rl&&r
7314
MC/DC100%2/2
$delete and $key =~ /__password/
$delete $key =~ /__password/
Truth table100%3/3
A and B
A= $deleteB= $key =~ /__password/
ABresult
0X0
100
111
60 4 for (keys %{$$self{__scaffolding}}) {
61 1 ▶ if ( ref $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$_}) 1 0 1
62 1 $self->{$_} = encrypt($self->{$_}, $self->{__scaffolding}{$_}, $self->{__cipher});
63 1 delete $self->{__scaffolding}{$_};
64 }
65 }
66 }
67 78 ▶ delete $$self{$key} if $delete;
Branch: 1 branch
BranchTrueFalseTotal
if $delete 5 73 78
68 78 ▶ return $self->{$key} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 73 5 78
69 5 return $self->{$key};
70 # SECRET FIELD
71 ▶ } elsif ( $key =~ m/^(_{1}[^_][^:]*)$/ ||$key =~ m/.*?::(_{1}[^_][^:]*)/ ) {
Condition66%2/3
$key =~ /^(_{1}[^_][^:]*)$/ or $key =~ /.*?::(_{1}[^_][^:]*)/
l!l&&r!l&&!r
573064
MC/DC50%1/2
$key =~ /^(_{1}[^_][^:]*)$/ or $key =~ /.*?::(_{1}[^_][^:]*)/
$key =~ /^(_{1}[^_][^:]*)$/ $key =~ /.*?::(_{1}[^_][^:]*)/
Truth table66%2/3
A or B
A= $key =~ /^(_{1}[^_][^:]*)$/B= $key =~ /.*?::(_{1}[^_][^:]*)/
ABresult
1X1
011
000
72 573 my $ctext = $self->{$1};
73 573 ▶ if ( ref $ctext && !($value)) { # ENCRYPT REF AT FETCH
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ctext and not $value) 179 394 573
Condition66%2/3
ref $ctext and not $value
!ll&&!rl&&r
3940179
MC/DC50%1/2
ref $ctext and not $value
ref $ctext not $value
Truth table66%2/3
A and not(B)
A= ref $ctextB= not $value
ABresult
0X0
100
111
74 179 ▶ my $pass = $self->{__scaffolding}{$1} || $self->{__password};
Condition33%1/3
$self->{'__scaffolding'}{$1} || $self->{'__password'}
l!l&&r!l&&!r
17900
MC/DC0%0/2
$self->{'__scaffolding'}{$1} || $self->{'__password'}
$self->{'__scaffolding'}{$1} $self->{'__password'}
Truth table33%1/3
A || B
A= $self->{'__scaffolding'}{$1}B= $self->{'__password'}
ABresult
1X1
011
000
75 179 ▶ return undef unless $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless $pass 0 179 179
76 179 $self->{$1} = encrypt($ctext, $pass, $self->{__cipher});
77 179 return $self->FETCH ($1);
78 }
79 394 my $ptext = qq{}; my $isnot = !( exists $self->{$1} );
80 394 my $auth = verify($self,$1);
81 394 ▶ return undef if !($auth) && ref $self->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if not $auth and ref $self->{$1} 0 394 394
Condition66%2/3
not $auth and ref $self->{$1}
!ll&&!rl&&r
299950
MC/DC0%0/2
not $auth and ref $self->{$1}
not $auth ref $self->{$1}
Truth table66%2/3
not(A) and B
A= not $authB= ref $self->{$1}
ABresult
0X0
100
111
82 394 ▶ return undef if !($auth) && $self->{__hide};
Branch: 1 branch
BranchTrueFalseTotal
if not $auth and $self->{"__hide"} 6 388 394
Condition100%3/3
not $auth and $self->{"__hide"}
!ll&&!rl&&r
299896
MC/DC100%2/2
not $auth and $self->{"__hide"}
not $auth $self->{"__hide"}
Truth table100%3/3
not(A) and B
A= not $authB= $self->{"__hide"}
ABresult
0X0
100
111
83 388 ▶ if ($auth && $auth ne "1") { $ptext = $auth }
Branch: 1 branch
BranchTrueFalseTotal
if ($auth and $auth ne 1) 196 192 388
Condition100%3/3
$auth and $auth ne 1
!ll&&!rl&&r
89103196
MC/DC100%2/2
$auth and $auth ne 1
$auth $auth ne 1
Truth table100%3/3
A and B
A= $authB= $auth ne 1
ABresult
0X0
100
111
84 388 ▶ if ($value && $auth) { # STORE
Branch: 5 branches
BranchTrueFalseTotal
if ($value and $auth) { } 65 323 388
elsif ($auth and $delete) { } 4 319 323
elsif ($isnot and not $value) { } 46 273 319
elsif (not $auth and $ctext) { } 89 184 273
elsif ($auth and not $isnot) { } 184 0 184
Condition66%2/3
$value and $auth
!ll&&!rl&&r
323065
MC/DC50%1/2
$value and $auth
$value $auth
Truth table66%2/3
A and B
A= $valueB= $auth
ABresult
0X0
100
111
85 65 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 42 23 65
86 42 $self->{__scaffolding}{$1} = $self->{__password}; $ctext = $value;
87 } else {
88 23 my $key = $1;
89 23 ▶ unless ($self->{__password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"__password"}) 1 22 23
90 1 ▶ if ($value =~ m:^\S+\s\S{22}\s:) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\S+\s\S{22}\s/) { } 1 0 1
91 1 return $self->{$key} = $value;
92 0 } else { return undef }
93 }
94 22 $ctext = encrypt($value, $self->{__password}, $self->{__cipher});
95 }
96 64 $self->{$1} = $ctext;
97 64 return $value;
98 ▶ } elsif ($auth && $delete) { # DELETE
Condition100%3/3
$auth and $delete
!ll&&!rl&&r
892304
MC/DC100%2/2
$auth and $delete
$auth $delete
Truth table100%3/3
A and B
A= $authB= $delete
ABresult
0X0
100
111
99 4 delete $$self{$1}
100 ▶ } elsif ($isnot && (!($value))) { # DOESN'T EXIST
Condition66%2/3
$isnot and not $value
!ll&&!rl&&r
273046
MC/DC50%1/2
$isnot and not $value
$isnot not $value
Truth table66%2/3
A and not(B)
A= $isnotB= not $value
ABresult
0X0
100
111
101 46 return;
102 ▶ } elsif ((!($auth)) && $ctext) {
Condition66%2/3
not $auth and $ctext
!ll&&!rl&&r
184089
MC/DC50%1/2
not $auth and $ctext
not $auth $ctext
Truth table66%2/3
not(A) and B
A= not $authB= $ctext
ABresult
0X0
100
111
103 89 return $ctext; # FETCH return ciphertext
104 ▶ } elsif ($auth && !($isnot)) { # FETCH return plaintext
Condition33%1/3
$auth and not $isnot
!ll&&!rl&&r
00184
MC/DC0%0/2
$auth and not $isnot
$auth not $isnot
Truth table33%1/3
A and not(B)
A= $authB= not $isnot
ABresult
0X0
100
111
105 184 ▶ if (ref $ptext) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ptext) 167 17 184
106 167 $self->{$1} = $ptext;
107 167 $self->{__scaffolding}{$1} = $self->{__password}; # Ref counting mechanism
108 167 return $self->{$1};
109 }
110 }
111 21 ▶ return undef unless $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth 0 21 21
112 21 return $ptext;
113 # PUBLIC FIELD
114 ▶ } elsif ( $key =~ m/([^:]*)$/ || $key =~ m/.*?::([^:]*)/ ) {
Condition33%1/3
$key =~ /([^:]*)$/ or $key =~ /.*?::([^:]*)/
l!l&&r!l&&!r
6400
MC/DC0%0/2
$key =~ /([^:]*)$/ or $key =~ /.*?::([^:]*)/
$key =~ /([^:]*)$/ $key =~ /.*?::([^:]*)/
Truth table33%1/3
A or B
A= $key =~ /([^:]*)$/B= $key =~ /.*?::([^:]*)/
ABresult
1X1
011
000
115 64 ▶ $self->{$1} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 20 44 64
116 64 ▶ delete $$self{$1} if $delete;
Branch: 1 branch
BranchTrueFalseTotal
if $delete 4 60 64
117 64 ▶ return $self->{$1} if $self->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$1} 48 16 64
118 16 return undef;
119 }
120  
121 }
122  
123 202 ▶sub encrypt { # ($plaintext, $password, $cipher)
Subroutine encrypt: called CC=2 SCAR=6.9
Pod undocumented
124 202 ▶ $_[0] = qq{REF }. Data::Dumper->new([$_[0]])->Indent(0)->Terse(0)->Purity(1)->Dumpxs if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 180 22 202
125 202 return qq{$_[2] } . md5_base64($_[0]) .qq{ } .
126 Crypt::CBC->new($_[1],$_[2])->encrypt_hex($_[0])
127 }
128  
129 299 ▶sub decrypt { # ($cipher $md5sum $ciphertext, $password)
Subroutine decrypt: called CC=4 SCAR=13.9
Pod undocumented
130 299 ▶ return undef unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 7 292 299
131 292 my ($m, $d, $c) = split /\s/,$_[0];
132 292 my $ptext = Crypt::CBC->new($_[1],$m)->decrypt_hex($c);
133 292 my $check = md5_base64($ptext);
134 292 ▶ if ( $d eq $check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq $check) 200 92 292
135 200 ▶ if ($ptext =~ /^REF (.*)/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptext =~ /^REF (.*)/is) 167 33 200
136 167 my ($VAR1,$VAR2,$VAR3,$VAR4,$VAR5,$VAR6,$VAR7,$VAR8);
137 167 return eval qq{$1};
138 }
139 33 return $ptext;
140 }
141 }
142  
143 418 ▶sub verify { # ($self, $key)
Subroutine verify: called CC=6 SCAR=18.2
Pod undocumented
144 418 my ($self, $key) = splice @_,0,2;
145 # debug ("$self->{__scaffolding}{$key}, $self->{__password}, $self->{$key}");
146 418 ▶ return 1 unless $key =~ m:^_:;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^_/ 16 402 418
147 402 ▶ return 1 unless exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 103 299 402
148 return undef if ref $self->{$key} && ($self->{__scaffolding}{$key} ne
149 299 ▶ $self->{__password});
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{$key} and $self->{"__scaffolding"}{$key} ne $self->{"__password"} 0 299 299
Condition33%1/3
ref $self->{$key} and $self->{"__scaffolding"}{$key} ne $self->{"__password"}
!ll&&!rl&&r
29900
MC/DC0%0/2
ref $self->{$key} and $self->{"__scaffolding"}{$key} ne $self->{"__password"}
ref $self->{$key} $self->{"__scaffolding"}{$key} ne $self->{"__password"}
Truth table33%1/3
A and B
A= ref $self->{$key}B= $self->{"__scaffolding"}{$key} ne $self->{"__password"}
ABresult
0X0
100
111
150 299 my $ptext = decrypt($self->{$key}, $self->{__password});
151 299 ▶ return $ptext if $ptext;
Branch: 1 branch
BranchTrueFalseTotal
if $ptext 200 99 299
152 }
153
154 0 ▶sub each { CORE::each %{$_[0]} }
Subroutine each: not called CC=1 SCAR=6.9
Pod undocumented
155 0 ▶sub keys { CORE::keys %{$_[0]} }
Subroutine keys: not called CC=1 SCAR=6.9
Pod undocumented
156 0 ▶sub values { CORE::values %{$_[0]} }
Subroutine values: not called CC=1 SCAR=6.9
Pod undocumented
157 0 ▶sub exists { CORE::exists $_[0]->{$_[1]} }
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
158  
159 sub TIEHASH # ($class, @args)
160 12 ▶{
Subroutine TIEHASH: called CC=4 SCAR=14.0
161 12 ▶ my $class = ref($_[0]) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
162 12 my $self = bless {}, $class;
163 12 ▶ $self->{__password} = $_[1] if $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 6 6 12
164 12 ▶ $self->{__cipher} = $_[2] || qq{Blowfish};
Condition100%2/2
$_[2] || 'Blowfish'
l!l
57
MC/DC100%1/1
$_[2] || 'Blowfish'
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
165 12 return $self;
166 }
167  
168 sub FETCH # ($self, $key)
169 434 ▶{
Subroutine FETCH: called CC=2 SCAR=6.9
170 434 my ($self, $key) = @_;
171 434 my $entry = _access($self,$key,(caller)[0..1]);
172 434 ▶ return $entry if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 394 40 434
173 }
174  
175 sub STORE # ($self, $key, $value)
176 158 ▶{
Subroutine STORE: called CC=2 SCAR=7.1
177 158 my ($self, $key, $value) = @_;
178 158 my $entry = _access($self,$key,(caller)[0..1],$value);
179 158 ▶ return $entry if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 158 0 158
180 }
181  
182 sub DELETE # ($self, $key)
183 13 ▶{
Subroutine DELETE: called CC=1 SCAR=0.0
184 13 my ($self, $key) = @_;
185 13 return _access($self,$key,(caller)[0..1],'',1);
186 }
187  
188 sub CLEAR # ($self)
189 8 ▶{
Subroutine CLEAR: called CC=2 SCAR=6.9
190 8 my ($self) = @_;
191 24 return undef if grep { ! $self->verify($_) }
192 8 ▶ grep { ! /__/ } CORE::keys %{$self};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $self->verify($_);} grep({not /__/;} CORE::keys %{$self;}) 4 4 8
193 4 %{$self} = ();
194 }
195  
196 sub EXISTS # ($self, $key)
197 40 ▶{
Subroutine EXISTS: called CC=2 SCAR=6.9
198 40 my ($self, $key) = @_;
199 40 my @context = (caller)[0..1];
200 40 ▶ return _access($self,$key,@context) ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
_access($self, $key, @context) ? : 16 24 40
201 }
202  
203 sub FIRSTKEY # ($self)
204 10 ▶{
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
205 10 my ($self) = @_;
206 10 CORE::keys %{$self};
207 10 goto &NEXTKEY;
208 }
209  
210 sub NEXTKEY # ($self)
211 80 ▶{
Subroutine NEXTKEY: called CC=3 SCAR=11.0
212 80 my $self = $_[0]; my $key;
213 80 my @context = (caller)[0..1];
214 80 ▶ while (defined($key = CORE::each %{$self})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($key = CORE::each %{$self;})) 96 10 106
215 96 ▶ last if eval { _access($self,$key,@context) }
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { _access($self, $key, @context) } } 70 26 96
216 }
217 80 return $key;
218 }
219  
220 sub DESTROY # ($self)
221 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
222 }
223  
224 7 1;
225 __END__
↑ Summary