blib/lib/Crypt/xDBM_File.pm

filter: Statementstmt 81.2% 65 / 80 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 76.9% 10 / 13 Podpod n/a totaltotal 77.5% 83 / 107 CC 8 SCAR 8.8
CC
8
Cov
78%
NEXTKEY
6.0
TIEHASH
3.0
_encrypt_string
3.0
CRAP 2.4
↑ Summary
1 package Crypt::xDBM_File;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $VERSION = '1.02';
7  
8 # Preloaded methods go here.
9  
10 11 ▶sub _encrypt_string {
Subroutine _encrypt_string: called CC=3 SCAR=11.0
11 11 my ($self, $string, $block_size) = @_;
12 11 my ($i, $len, $tmp_string, $crypt_string);
13  
14 11 $string .= "+"; # pad marker
15 11 $len = $block_size - (length($string) % $block_size);
16 11 ▶ if ($len != $block_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != $block_size) 11 0 11
17 11 $string .= "\0" x $len;
18 }
19 11 $len = length($string);
20 11 $crypt_string = "";
21 11 ▶ for ($i=0; $i < $len; $i += $block_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 14 11 25
22 14 $tmp_string = $self->{'cipher'}->encrypt(substr($string,
23 $i, $block_size));
24 14 $crypt_string .= $tmp_string;
25 }
26 11 return $crypt_string;
27 }
28  
29 3 ▶sub _decrypt_string { # should already be padded to block size
Subroutine _decrypt_string: called CC=2 SCAR=6.9
30 3 my ($self, $crypted_string, $block_size) = @_;
31 3 my ($i, $len, $tmp_string, $string);
32  
33 3 $len = length($crypted_string);
34 3 $string = "";
35 3 ▶ for ($i=0; $i < $len; $i += $block_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 6 3 9
36 6 $tmp_string = $self->{'cipher'}->decrypt(substr($crypted_string,
37 $i, $block_size));
38 6 $string .= $tmp_string;
39 }
40 3 return (substr($string, 0, rindex($string, "+")));
41 }
42  
43 1 ▶sub TIEHASH { # associate hash variable to these routines
Subroutine TIEHASH: called CC=3 SCAR=11.0
44 1 my ($pkg) = shift @_;
45 1 my $self = {};
46  
47 1 $self->{'crypt_method'} = shift @_;
48 1 $self->{'key'} = shift @_;
49  
50 1 $self->{'key_pad'} = keysize {$self->{'crypt_method'}};
51 1 ▶ if ($self->{'key_pad'} == 0) { # pad to 8 byte boundary by default
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key_pad"} == 0) 1 0 1
52 1 $self->{'key_pad'} = 8;
53 }
54 1 $self->{'block_pad'} = blocksize {$self->{'crypt_method'}};
55 1 ▶ if ($self->{'block_pad'} == 0) { # pad to 8 byte boundary by default
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"block_pad"} == 0) 0 1 1
56 0 $self->{'block_pad'} = 8;
57 }
58 # print "key_pad = [$self->{'key_pad'}]\n";
59 # print "block_pad = [$self->{'block_pad'}]\n";
60 # print "crypt method [$self->{'crypt_method'}], key [$self->{'key'}]\n";
61  
62 1 my $len = length($self->{'key'}) % $self->{'key_pad'};
63 1 $self->{'key'} .= ' ' x ($self->{'key_pad'} - $len);
64 1 $self->{'key'} = substr($self->{'key'}, 0, $self->{'key_pad'});
65 1 $self->{'cipher'} = new {$self->{'crypt_method'}} $self->{'key'};
66 1 tie %{$self->{'localhash'}}, shift @_, @_;
67 1 return (bless $self, $pkg);
68 }
69  
70 3 ▶sub FETCH { # get an encrypted item and decrypt it
Subroutine FETCH: called CC=2 SCAR=7.2
71 3 my ($self, $key) = @_;
72 3 my $crypted_key = $self->_encrypt_string($key, $self->{'block_pad'});
73 3 my $crypted_value = $self->{'localhash'}{$crypted_key};
74 3 ▶ if (defined($crypted_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $crypted_value) { } 3 0 3
75 3 return ($self->_decrypt_string($crypted_value, $self->{'block_pad'}));
76 } else {
77 0 return;
78 }
79 }
80  
81 3 ▶sub STORE { # get an encrypted item and decrypt it
Subroutine STORE: called CC=1 SCAR=0.0
82 3 my ($self, $key, $value) = @_;
83 3 my $crypted_key = $self->_encrypt_string($key, $self->{'block_pad'});
84 3 my $crypted_value = $self->_encrypt_string($value, $self->{'block_pad'});
85 3 return ($self->{'localhash'}{$crypted_key} = $crypted_value);
86 }
87  
88 1 ▶sub DELETE { # delete an item
Subroutine DELETE: called CC=1 SCAR=0.0
89 1 my ($self, $key) = @_;
90 1 my $crypted_key = $self->_encrypt_string($key, $self->{'block_pad'});
91  
92 1 return (delete $self->{'localhash'}{$crypted_key});
93 }
94  
95 1 ▶sub EXISTS { # does it exist
Subroutine EXISTS: called CC=1 SCAR=0.0
96 1 my ($self, $key) = @_;
97 1 my $crypted_key = $self->_encrypt_string($key, $self->{'block_pad'});
98  
99 1 return (exists $self->{'localhash'}{$crypted_key});
100 }
101  
102 0 ▶sub FIRSTKEY { # first key request
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
103 0 my $self = shift;
104 0 my ($key, $crypted_key);
105  
106 0 keys(%{$self->{'localhash'}}); # reset eachness
107 0 return($self->NEXTKEY());
108 }
109  
110 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
111 0 my $self = shift;
112 0 my $crypted_key = each (%{$self->{'localhash'}});
113 0 ▶ if (defined $crypted_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $crypted_key) { } 0 0 0
114 0 return ($self->_decrypt_string($crypted_key, $self->{'block_pad'}));
115 } else {
116 0 return;
117 }
118 }
119  
120 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
121 0 my $self = shift;
122 0 return ($self->{'localhash'} = ());
123 }
124  
125 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
126 1 my $self = shift;
127 1 return (untie %{$self->{'localhash'}});
128 }
129  
130  
131 # Autoload methods go after =cut, and are processed by the autosplit program.
132  
133 1 1;
134 __END__
↑ Summary