File Coverage

blib/lib/Crypt/xDBM_File.pm
Criterion Covered Total %
statement 63 78 80.7
branch 4 10 40.0
condition n/a
subroutine 10 13 76.9
pod n/a
total 77 101 76.2


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