File Coverage

blib/lib/Crypt/Mode/CBC.pm
Criterion Covered Total %
statement 15 16 93.7
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Crypt::Mode::CBC;
2              
3             ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY!
4              
5 21     21   169463 use strict;
  21         30  
  21         610  
6 21     21   69 use warnings;
  21         24  
  21         1116  
7             our $VERSION = '0.088_004';
8              
9 21     21   6853 use Crypt::Cipher;
  21         43  
  21         2974  
10              
11             sub encrypt {
12 62     62 1 351928 my ($self, $pt) = (shift, shift);
13 62         269 local $SIG{__DIE__} = \&CryptX::_croak;
14 62         911 $self->start_encrypt(@_)->add($pt) . $self->finish;
15             }
16              
17             sub decrypt {
18 63     63 1 1330 my ($self, $ct) = (shift, shift);
19 63         163 local $SIG{__DIE__} = \&CryptX::_croak;
20 63         578 $self->start_decrypt(@_)->add($ct) . $self->finish;
21             }
22              
23 0     0     sub CLONE_SKIP { 1 } # prevent cloning
24              
25             1;
26              
27             =pod
28              
29             =head1 NAME
30              
31             Crypt::Mode::CBC - Block cipher mode CBC [Cipher-block chaining]
32              
33             =head1 SYNOPSIS
34              
35             use Crypt::Mode::CBC;
36             my $m = Crypt::Mode::CBC->new('AES');
37             my $key = '1234567890123456';
38             my $iv = '1234567890123456';
39             my $plaintext = 'example plaintext';
40             my $chunk1 = 'example ';
41             my $chunk2 = 'plaintext';
42              
43             #(en|de)crypt at once
44             my $single_ciphertext = $m->encrypt($plaintext, $key, $iv);
45             my $single_plaintext = $m->decrypt($single_ciphertext, $key, $iv);
46              
47             #encrypt more chunks
48             $m->start_encrypt($key, $iv);
49             my $chunked_ciphertext = '';
50             $chunked_ciphertext .= $m->add($chunk1);
51             $chunked_ciphertext .= $m->add($chunk2);
52             $chunked_ciphertext .= $m->finish;
53              
54             #decrypt more chunks
55             $m->start_decrypt($key, $iv);
56             my $chunked_plaintext = '';
57             $chunked_plaintext .= $m->add($chunked_ciphertext);
58             $chunked_plaintext .= $m->finish;
59              
60             =head1 DESCRIPTION
61              
62             This module implements CBC cipher mode. B it works only with ciphers from L (Crypt::Cipher::NNNN).
63              
64             =head1 METHODS
65              
66             Unless noted otherwise, assume C<$m> is an existing mode object created via
67             C, for example:
68              
69             my $m = Crypt::Mode::CBC->new('AES');
70              
71             =head2 new
72              
73             my $m = Crypt::Mode::CBC->new($name);
74             #or
75             my $m = Crypt::Mode::CBC->new($name, $padding);
76             #or
77             my $m = Crypt::Mode::CBC->new($name, $padding, $cipher_rounds);
78              
79             # $name ....... [string] one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
80             # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
81             # 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64',
82             # 'SEED', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent'
83             # simply any for which there exists Crypt::Cipher::
84             # $padding .... [integer] 0 no padding (plaintext size has to be multiple of block length)
85             # 1 PKCS5 padding, Crypt::CBC's "standard" - DEFAULT
86             # 2 Crypt::CBC's "oneandzeroes"
87             # 3 ANSI X.923 padding
88             # 4 zero padding
89             # 5 zero padding (+a block of zeros if the output length is divisible by the blocksize)
90             # $cipher_rounds ... [integer] optional, num of rounds for given cipher
91              
92             =head2 encrypt
93              
94             Encrypts the plaintext in a single call. Returns the ciphertext as a binary string.
95             The plaintext scalar is converted to bytes using Perl's usual scalar
96             stringification. Defined scalars, including numbers and string-overloaded
97             objects, are accepted. C is treated as an empty string and may emit
98             Perl's usual "uninitialized value" warning.
99              
100             my $ciphertext = $m->encrypt($plaintext, $key, $iv);
101              
102             =head2 decrypt
103              
104             Decrypts the ciphertext in a single call. Returns the plaintext as a binary string.
105             The ciphertext scalar is converted to bytes using Perl's usual scalar
106             stringification. Defined scalars, including numbers and string-overloaded
107             objects, are accepted. C is treated as an empty string and may emit
108             Perl's usual "uninitialized value" warning.
109              
110             my $plaintext = $m->decrypt($ciphertext, $key, $iv);
111              
112             =head2 start_encrypt
113              
114             Initializes encryption mode. Returns the object itself.
115              
116             $m->start_encrypt($key, $iv);
117              
118             =head2 start_decrypt
119              
120             Initializes decryption mode. Returns the object itself.
121              
122             $m->start_decrypt($key, $iv);
123              
124             =head2 add
125              
126             Feeds data to the encryption or decryption stream. Returns a binary string.
127              
128             Each argument is converted to bytes using Perl's usual scalar stringification.
129             Defined scalars, including numbers and string-overloaded objects, are
130             accepted. C is treated as an empty string and may emit Perl's usual
131             "uninitialized value" warning.
132              
133             # in encrypt mode
134             my $ciphertext = $m->add($plaintext);
135              
136             # in decrypt mode
137             my $plaintext = $m->add($ciphertext);
138              
139             =head2 finish
140              
141             #encrypt more chunks
142             $m->start_encrypt($key, $iv);
143             my $chunk1 = 'example ';
144             my $chunk2 = 'plaintext';
145             my $ciphertext = '';
146             $ciphertext .= $m->add($chunk1);
147             $ciphertext .= $m->add($chunk2);
148             $ciphertext .= $m->finish;
149              
150             #decrypt more chunks
151             $m->start_decrypt($key, $iv);
152             my $plaintext = '';
153             $plaintext .= $m->add($ciphertext);
154             $plaintext .= $m->finish;
155              
156             =head1 SEE ALSO
157              
158             =over
159              
160             =item * L, L
161              
162             =item * L, L, ...
163              
164             =item * L
165              
166             =back
167              
168             =cut