blib/lib/LibWeb/Crypt.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 64 CC 3 SCAR 12.4
CC
3
Cov
0%
encrypt_cipher
6.0
decrypt_cipher
6.0
new
2.0
CRAP 3.5
1 #==============================================================================
2 # LibWeb::Crypt -- Encryption for libweb applications.
3  
4 package LibWeb::Crypt;
5  
6 # Copyright (C) 2000 Colin Kong
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #=============================================================================
22  
23 # $Id: Crypt.pm,v 1.4 2000/07/18 06:33:30 ckyc Exp $
24  
25 #-#############################
26 # Use standard library.
27 use strict;
28 use vars qw($VERSION @ISA);
29  
30 #-#############################
31 # Use custom library.
32 # This class should not require LibWeb::Core.
33 # You have been warned!.
34 require Crypt::CBC;
35 require LibWeb::Class;
36 ##require Crypt::Blowfish; Crypt::IDEA, or Crypt::DES;
37  
38 #-#############################
39 # Version.
40 $VERSION = '0.02';
41  
42 #-#############################
43 # Inheritance.
44 @ISA = qw(LibWeb::Class);
45  
46 #-#############################
47 # Methods.
48 sub new {
49 my ($class, $Class, $self);
50 $class = shift;
51 $Class = ref($class) || $class;
52 $self = $Class->SUPER::new(shift);
53 bless($self, $Class);
54 }
55  
56 sub DESTROY {}
57  
58 sub encrypt_cipher {
59 #
60 # Params: -data=>, -key=>, -algorithm=>, -format=>
61 # e.g. -algorithm => 'Crypt::Blowfish' / 'Crypt::DES' / 'Crypt::IDEA'
62 # e.g. -format => 'binary' / 'hex'.
63 # This makes use of Crypt::CBC module. Accept data of arbitrary length.
64 #
65 my ($self, $data, $key, $algorithm, $format, $cipher);
66 $self = shift;
67 ($data, $key, $algorithm, $format) =
68 $self->rearrange(['DATA', ,'KEY', 'ALGORITHM', 'FORMAT'], @_);
69  
70 # Makes perl taint mode happy. $1 is $cipherAlgorithm in disguise.
71 $algorithm =~ m:(.*):;
72 $cipher = new Crypt::CBC($key, $1);
73 return (uc($format) eq 'HEX') ?
74 $cipher->encrypt_hex($data) : $cipher->encrypt($data);
75 }
76  
77 sub decrypt_cipher {
78 #
79 # Params: -cipher=>, -key=>, -algorithm=>, -format=>
80 # e.g. -algorithm => 'Crypt::Blowfish' / 'Crypt::DES' / 'Crypt::IDEA'
81 # e.g. -format => 'binary' / 'hex'.
82 # This makes use of Crypt::CBC module. Accept cipher generated by
83 # encrypt_cipher() of this module (LibWeb).
84 #
85 my ($self, $cipherText, $key, $algorithm, $format, $cipher);
86 $self = shift;
87 ($cipherText, $key, $algorithm, $format) =
88 $self->rearrange(['CIPHER', 'KEY', 'ALGORITHM', 'FORMAT'], @_);
89  
90 # Makes perl taint mode happy. $1 is $cipherAlgorithm in disguise.
91 $algorithm =~ m:(.*):;
92 $cipher = new Crypt::CBC($key, $1);
93 return (uc($format) eq 'HEX') ?
94 $cipher->decrypt_hex($cipherText) : $cipher->decrypt($cipherText);
95 }
96  
97 sub encrypt_password {
98 #
99 # Params: $plainPassword.
100 #
101 # Encrypts argument (usually a password) and returns a
102 # 13-character long string. Random salt.
103 # This uses the perl's crypt(). May migrate to use
104 # MD5 in later release of this library.
105 #
106 my ($self, @salt_chars, $salt);
107 $self = shift;
108 @salt_chars = ('A'..'Z', 0..9, 'a'..'z','.','/');
109 $salt = join '',@salt_chars[rand 64, rand 64];
110 return crypt($_[0] ,$salt);
111 }
112  
113 1;
114 __END__