File Coverage

blib/lib/Net/SSH/Perl/Cipher/CFB.pm
Criterion Covered Total %
statement 30 30 100.0
branch n/a
condition 1 3 33.3
subroutine 5 5 100.0
pod 0 3 0.0
total 36 41 87.8


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