File Coverage

blib/lib/Net/SSH/Perl/Cipher/CBC.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition 2 3 66.6
subroutine 4 4 100.0
pod 0 3 0.0
total 35 39 89.7


line stmt bran cond sub pod time code
1             # This code is based in part on the Systemics Crypt::CBC.
2             # Parts copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
3             # All rights reserved.
4              
5             package Net::SSH::Perl::Cipher::CBC;
6 2     2   16 use strict;
  2         4  
  2         647  
7              
8             sub new {
9 37     37 0 110 my($class, $ciph, $iv) = @_;
10 37   66     348 bless {
11             cipher => $ciph,
12             iv => $iv || ("\0" x $ciph->blocksize),
13             }, $class;
14             }
15              
16             sub encrypt {
17 18     18 0 32 my $cbc = shift;
18 18         31 my $data = shift;
19              
20 18         26 my $retval = "";
21 18         31 my $iv = $cbc->{iv};
22 18         25 my $ciph = $cbc->{cipher};
23 18         53 my $size = $ciph->blocksize;
24              
25 18         43 while (length $data) {
26 18         51 my $in = substr($data, 0, $size, '') ^ $iv;
27 18         124 $iv = $ciph->encrypt($in);
28 18         52 $retval .= $iv;
29             }
30              
31 18         45 $cbc->{iv} = $iv;
32 18         69 $retval;
33             }
34              
35             sub decrypt {
36 19     19 0 36 my $cbc = shift;
37 19         28 my $data = shift;
38              
39 19         30 my $retval = "";
40 19         36 my $iv = $cbc->{iv};
41 19         28 my $ciph = $cbc->{cipher};
42 19         46 my $size = $ciph->blocksize;
43              
44 19         43 while (length $data) {
45 29         64 my $in = substr($data, 0, $size, '');
46 29         126 $retval .= $ciph->decrypt($in) ^ $iv;
47 29         73 $iv = $in;
48             }
49              
50 19         30 $cbc->{iv} = $iv;
51 19         68 $retval;
52             }
53              
54             1;
55             __END__