Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Crypt::Blowfish; |
2 | ||
3 | require Exporter; | |
4 | require DynaLoader; | |
5 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | |
6 | ||
7 | @ISA = qw(Exporter DynaLoader); | |
8 | # @ISA = qw(Exporter DynaLoader Crypt::BlockCipher); | |
9 | ||
10 | # Items to export into callers namespace by default | |
11 | @EXPORT = qw(); | |
12 | ||
13 | # Other items we are prepared to export if requested | |
14 | @EXPORT_OK = qw( | |
15 | blocksize keysize min_keysize max_keysize | |
16 | new encrypt decrypt | |
17 | ); | |
18 | ||
19 | $VERSION = '2.09'; | |
20 | bootstrap Crypt::Blowfish $VERSION; | |
21 | ||
22 | use strict; | |
23 | use Carp; | |
24 | ||
25 | sub usage | |
26 | { | |
27 | my ($package, $filename, $line, $subr) = caller(1); | |
28 | $Carp::CarpLevel = 2; | |
29 | croak "Usage: $subr(@_)"; | |
30 | } | |
31 | ||
32 | ||
33 | sub blocksize { 8; } # /* byte my shiny metal.. */ | |
34 | sub keysize { 0; } # /* we'll leave this at 8 .. for now. expect change. */ | |
35 | sub min_keysize { 8; } | |
36 | sub max_keysize { 56; } | |
37 | ||
38 | sub new | |
39 | { | |
40 | usage("new Blowfish key") unless @_ == 2; | |
41 | ||
42 | my $type = shift; my $self = {}; bless $self, $type; | |
43 | ||
44 | $self->{'ks'} = Crypt::Blowfish::init(shift); | |
45 | ||
46 | $self; | |
47 | } | |
48 | ||
49 | sub encrypt | |
50 | { | |
51 | usage("encrypt data[8 bytes]") unless @_ == 2; | |
52 | ||
53 | my $self = shift; | |
54 | my $data = shift; | |
55 | ||
56 | Crypt::Blowfish::crypt($data, $data, $self->{'ks'}, 0); | |
57 | ||
58 | $data; | |
59 | } | |
60 | ||
61 | sub decrypt | |
62 | { | |
63 | usage("decrypt data[8 bytes]") unless @_ == 2; | |
64 | ||
65 | my $self = shift; | |
66 | my $data = shift; | |
67 | ||
68 | Crypt::Blowfish::crypt($data, $data, $self->{'ks'}, 1); | |
69 | ||
70 | $data; | |
71 | } | |
72 | ||
73 | 1; | |
74 | ||
75 | __END__ | |
76 | # | |
77 | # Parts Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/) | |
78 | # New Parts Copyright (C) 2000, 2001 W3Works, LLC (http://www.w3works.com/) | |
79 | # All rights reserved. | |
80 | # | |
81 | ||
82 | =head1 NAME | |
83 | ||
84 | Crypt::Blowfish - Perl Blowfish encryption module | |
85 | ||
86 | =head1 SYNOPSIS | |
87 | ||
88 | use Crypt::Blowfish; | |
89 | my $cipher = new Crypt::Blowfish $key; | |
90 | my $ciphertext = $cipher->encrypt($plaintext); | |
91 | my $plaintext = $cipher->decrypt($ciphertext); | |
92 | ||
93 | =head1 DESCRIPTION | |
94 | ||
95 | Blowfish is capable of strong encryption and can use key sizes up | |
96 | to 56 bytes (a 448 bit key). You're encouraged to take advantage | |
97 | of the full key size to ensure the strongest encryption possible | |
98 | from this module. | |
99 | ||
100 | Crypt::Blowfish has the following methods: | |
101 | ||
102 | =over 4 | |
103 | ||
104 | blocksize() | |
105 | keysize() | |
106 | encrypt() | |
107 | decrypt() | |
108 | ||
109 | =back | |
110 | ||
111 | =head1 FUNCTIONS | |
112 | ||
113 | =over 4 | |
114 | ||
115 | =item blocksize | |
116 | ||
117 | Returns the size (in bytes) of the block cipher. | |
118 | ||
119 | Crypt::Blowfish doesn't return a key size due to its ability | |
120 | to use variable-length keys. (well, more accurately, it won't | |
121 | as of 2.09 .. for now, it does. expect that to change) | |
122 | ||
123 | =item new | |
124 | ||
125 | my $cipher = new Crypt::Blowfish $key; | |
126 | ||
127 | This creates a new Crypt::Blowfish BlockCipher object, using $key, | |
128 | where $key is a key of C<keysize()> bytes (minimum of eight bytes). | |
129 | ||
130 | =item encrypt | |
131 | ||
132 | my $cipher = new Crypt::Blowfish $key; | |
133 | my $ciphertext = $cipher->encrypt($plaintext); | |
134 | ||
135 | This function encrypts $plaintext and returns the $ciphertext | |
136 | where $plaintext and $ciphertext must be of C<blocksize()> bytes. | |
137 | (hint: Blowfish is an 8 byte block cipher) | |
138 | ||
139 | =item decrypt | |
140 | ||
141 | my $cipher = new Crypt::Blowfish $key; | |
142 | my $plaintext = $cipher->decrypt($ciphertext); | |
143 | ||
144 | This function decrypts $ciphertext and returns the $plaintext | |
145 | where $plaintext and $ciphertext must be of C<blocksize()> bytes. | |
146 | (hint: see previous hint) | |
147 | ||
148 | =back | |
149 | ||
150 | =head1 EXAMPLE | |
151 | ||
152 | my $key = pack("H16", "0123456789ABCDEF"); # min. 8 bytes | |
153 | my $cipher = new Crypt::Blowfish $key; | |
154 | my $ciphertext = $cipher->encrypt("plaintex"); # SEE NOTES | |
155 | print unpack("H16", $ciphertext), "\n"; | |
156 | ||
157 | =head1 PLATFORMS | |
158 | ||
159 | Please see the README document for platforms and performance | |
160 | tests. | |
161 | ||
162 | =head1 NOTES | |
163 | ||
164 | The module is capable of being used with Crypt::CBC. You're | |
165 | encouraged to read the perldoc for Crypt::CBC if you intend to | |
166 | use this module for Cipher Block Chaining modes. In fact, if | |
167 | you have any intentions of encrypting more than eight bytes of | |
168 | data with this, or any other block cipher, you're going to need | |
169 | B<some> type of block chaining help. Crypt::CBC tends to be | |
170 | very good at this. If you're not going to encrypt more than | |
171 | eight bytes, your data B<must> be B<exactly> eight bytes long. | |
172 | If need be, do your own padding. "\0" as a null byte is perfectly | |
173 | valid to use for this. Additionally, the current maintainer for | |
174 | Crypt::Blowfish may or may not release Crypt::CBC_R which | |
175 | replaces the default 'RandomIV' initialization vector in | |
176 | Crypt::CBC with a random initialization vector. (to the limits | |
177 | of /dev/urandom and associates) In either case, please email | |
178 | amused@pobox.com for Crypt::CBC_R. | |
179 | ||
180 | =head1 SEE ALSO | |
181 | ||
182 | Crypt::CBC, | |
183 | Crypt::DES, | |
184 | Crypt::IDEA | |
185 | ||
186 | Bruce Schneier, I<Applied Cryptography>, 1995, Second Edition, | |
187 | published by John Wiley & Sons, Inc. | |
188 | ||
189 | =head1 COPYRIGHT | |
190 | ||
191 | The implementation of the Blowfish algorithm was developed by, | |
192 | and is copyright of, A.M. Kuchling. | |
193 | ||
194 | Other parts of the perl extension and module are | |
195 | copyright of Systemics Ltd ( http://www.systemics.com/ ). | |
196 | ||
197 | Code revisions, updates, and standalone release are copyright | |
198 | 1999-2001 W3Works, LLC. | |
199 | ||
200 | =head1 AUTHOR | |
201 | ||
202 | Original algorithm, Bruce Shneier. Original implementation, A.M. | |
203 | Kuchling. Original Perl implementation, Systemics Ltd. Current | |
204 | maintenance by W3Works, LLC. | |
205 | ||
206 | Current revision and maintainer: Dave Paris <amused@pobox.com> | |
207 |