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 24     24   247008 use strict;
  24         43  
  24         922  
6 24     24   118 use warnings;
  24         34  
  24         1722  
7             our $VERSION = '0.089';
8              
9 24     24   10785 use Crypt::Cipher;
  24         67  
  24         8069  
10              
11             sub encrypt {
12 62     62 1 429570 my ($self, $pt) = (shift, shift);
13 62         359 local $SIG{__DIE__} = \&CryptX::_croak;
14 62         1097 $self->start_encrypt(@_)->add($pt) . $self->finish;
15             }
16              
17             sub decrypt {
18 64     64 1 2022 my ($self, $ct) = (shift, shift);
19 64         201 local $SIG{__DIE__} = \&CryptX::_croak;
20 64         707 $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             # encrypt or decrypt in one call
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             # or any for which there is a Crypt::Cipher:: module
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, number 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             Finalizes the stream. In encryption mode, pads and encrypts the final block;
142             in decryption mode, decrypts and removes padding from the final block.
143             Returns the result as a binary string (may be empty if there is no data to flush).
144              
145             # encrypt more chunks
146             $m->start_encrypt($key, $iv);
147             my $chunk1 = 'example ';
148             my $chunk2 = 'plaintext';
149             my $ciphertext = '';
150             $ciphertext .= $m->add($chunk1);
151             $ciphertext .= $m->add($chunk2);
152             $ciphertext .= $m->finish;
153              
154             # decrypt more chunks
155             $m->start_decrypt($key, $iv);
156             my $plaintext = '';
157             $plaintext .= $m->add($ciphertext);
158             $plaintext .= $m->finish;
159              
160             =head1 SEE ALSO
161              
162             =over
163              
164             =item * L, L
165              
166             =item * L, L, ...
167              
168             =item * L
169              
170             =back
171              
172             =cut