blib/lib/Crypt/Cipher.pm

filter: Statementstmt 94.1% 16 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 91.6% 22 / 24 CC 1 SCAR 1.5
CC
1
Cov
94%
CLONE_SKIP
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Crypt/AuthEnc/GCM.pm ↑ Summary blib/lib/Crypt/Mode/CBC.pm »
1 package Crypt::Cipher;
2  
3 67 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 67 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 67 our $VERSION = '0.091';
6  
7 67 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 67 $Carp::Internal{(__PACKAGE__)}++;
9 67 ▶use CryptX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ### the following methods/functions are implemented in XS:
12 # - new
13 # - DESTROY
14 # - blocksize
15 # - decrypt
16 # - default_rounds
17 # - encrypt
18 # - max_keysize
19 # - min_keysize
20  
21 173 ▶sub keysize { goto \&max_keysize; } # for Crypt::CBC compatibility
Subroutine keysize: called CC=1 SCAR=0.0
Pod documented
22  
23 0 ▶sub CLONE_SKIP { 1 } # prevent cloning
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
24  
25 67 1;
26  
27 =pod
28  
29 =head1 NAME
30  
31 Crypt::Cipher - Generic interface to cipher functions
32  
33 =head1 SYNOPSIS
34  
35 #### example 1 (encrypting single block)
36 use Crypt::Cipher;
37  
38 my $key = '1234567890abcdef'; # 16 bytes, valid for AES-128
39 my $plaintext_block = '1234567890123456'; # one AES block (16 bytes)
40 my $c = Crypt::Cipher->new('AES', $key);
41 my $blocksize = $c->blocksize;
42 my $ciphertext = $c->encrypt($plaintext_block); # encrypt 1 block
43 my $plaintext = $c->decrypt($ciphertext); #decrypt 1 block
44  
45 ### example 2 (using CBC mode)
46 use Crypt::Mode::CBC;
47  
48 my $cbc_key = '1234567890abcdef'; # 16 bytes, valid for AES-128
49 my $iv = 'fedcba0987654321'; # 16 bytes
50 my $cbc = Crypt::Mode::CBC->new('AES');
51 my $cbc_ciphertext = $cbc->encrypt("secret data", $cbc_key, $iv);
52  
53 #### example 3 (compatibility with Crypt::CBC)
54 use Crypt::CBC;
55 use Crypt::Cipher;
56  
57 my $compat_key = '1234567890abcdef'; # 16 bytes, valid for AES-128
58 my $compat_iv = 'fedcba0987654321'; # 16 bytes
59 my $cipher = Crypt::Cipher->new('AES', $compat_key);
60 my $compat_cbc = Crypt::CBC->new( -cipher=>$cipher, -iv=>$compat_iv );
61 my $compat_ciphertext = $compat_cbc->encrypt("secret data");
62  
63 =head1 DESCRIPTION
64  
65 Provides an interface to various symmetric cipher algorithms.
66  
67 B<Note:> This module only implements single-block encryption and decryption.
68 For general data, use a block mode such as
69 L<Crypt::Mode::CBC>, L<Crypt::Mode::CTR>, or L<Crypt::CBC> (which is slower).
70  
71 =head1 METHODS
72  
73 Unless noted otherwise, assume C<$c> is an existing cipher object created via
74 C<new>, for example:
75  
76 my $c = Crypt::Cipher->new('AES', '1234567890abcdef');
77  
78 =head2 new
79  
80 Constructor. Returns a reference to the cipher object.
81  
82 ## basic scenario
83 my $c = Crypt::Cipher->new($name, $key);
84 # $name = one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
85 # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
86 # 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64',
87 # 'SEED', 'SM4', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent'
88 # or any <NAME> for which there is a Crypt::Cipher::<NAME> module
89 # $key = binary key (keysize should comply with selected cipher requirements)
90  
91 ## some of the ciphers (e.g. MULTI2, RC5, SAFER) allow one to set number of rounds
92 my $c = Crypt::Cipher->new('MULTI2', $key, $rounds);
93 # $rounds = positive integer (should comply with selected cipher requirements)
94  
95 =head2 encrypt
96  
97 Encrypts C<$plaintext> and returns C<$ciphertext>. An empty string is
98 accepted and returned unchanged; otherwise C<$plaintext> must be exactly
99 B<blocksize> bytes long.
100  
101 my $ciphertext = $c->encrypt($plaintext);
102  
103 =head2 decrypt
104  
105 Decrypts C<$ciphertext> and returns C<$plaintext>. An empty string is
106 accepted and returned unchanged; otherwise C<$ciphertext> must be exactly
107 B<blocksize> bytes long.
108  
109 my $plaintext = $c->decrypt($ciphertext);
110  
111 =head2 keysize
112  
113 Just an alias for B<max_keysize> (needed for L<Crypt::CBC> compatibility).
114  
115 =head2 max_keysize
116  
117 Returns the maximum allowed key size in bytes for the given cipher.
118  
119 $c->max_keysize;
120 #or
121 Crypt::Cipher->max_keysize('AES');
122 #or
123 Crypt::Cipher::max_keysize('AES');
124  
125 =head2 min_keysize
126  
127 Returns the minimum allowed key size in bytes for the given cipher.
128  
129 $c->min_keysize;
130 #or
131 Crypt::Cipher->min_keysize('AES');
132 #or
133 Crypt::Cipher::min_keysize('AES');
134  
135 =head2 blocksize
136  
137 Returns the block size in bytes for the given cipher.
138  
139 $c->blocksize;
140 #or
141 Crypt::Cipher->blocksize('AES');
142 #or
143 Crypt::Cipher::blocksize('AES');
144  
145 =head2 default_rounds
146  
147 Returns the default number of rounds for the given cipher. Only some ciphers,
148 such as MULTI2, RC5, and SAFER, let you set the number of rounds via C<new()>.
149  
150 $c->default_rounds;
151 #or
152 Crypt::Cipher->default_rounds('AES');
153 #or
154 Crypt::Cipher::default_rounds('AES');
155  
156 =head1 SEE ALSO
157  
158 =over
159  
160 =item * L<CryptX>
161  
162 =item * See subclasses such as L<Crypt::Cipher::AES>, L<Crypt::Cipher::Blowfish>, ...
163  
164 =back
165  
166 =cut
« blib/lib/Crypt/AuthEnc/GCM.pm ↑ Summary blib/lib/Crypt/Mode/CBC.pm »