Framework/Crypt.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Framework/Auth.pm ↑ Summary Framework/Format.pm »
1 #!/usr/bin/perl
2  
3 package Net::Server::Framework::Crypt;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Switch;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Crypt::CBC;
10 use MIME::Base64;
11 use Digest::MD5 qw(md5_hex);
12  
13 our ($VERSION) = '1.0';
14  
15 sub encrypt {
16 my ( $message, $key, $type, $ascii ) = @_;
17  
18 my $cipher;
19 $key = 'CHANGE THIS KEY TO SOMETHING SECRET!'
20 unless defined $key;
21 $type = 'blowfish' unless defined $type;
22 switch ($type) {
23 case "blowfish" { $cipher = 'Blowfish'; }
24 }
25 my $c = Crypt::CBC->new(
26 -key => $key,
27 -cipher => $cipher,
28 );
29 my $enc = $c->encrypt($message);
30 return $enc
31 unless defined $ascii;
32 return encode_base64($enc);
33  
34 }
35  
36 sub decrypt {
37 my ( $message, $key, $type, $ascii ) = @_;
38  
39 my $cipher;
40 $type = 'blowfish' unless defined $type;
41 $key = 'CHANGE THIS KEY TO SOMETHING SECRET!'
42 unless defined $key;
43 switch ($type) {
44 case "blowfish" { $cipher = 'Blowfish'; }
45 }
46 my $c = Crypt::CBC->new(
47 -key => $key,
48 -cipher => $cipher,
49 );
50 return $c->decrypt($message)
51 unless defined $ascii;
52 return $c->decrypt( decode_base64($message) );
53 }
54  
55 sub hash {
56 my $message = shift;
57  
58 return md5_hex($message);
59 }
60  
61 1;
62  
63 =head1 NAME
64  
65 Net::Server::Framework::Crypt - a wrapper for several encryption libs
66  
67  
68 =head1 VERSION
69  
70 This documentation refers to C<Net::Server::Framework::Crypt> version 1.0.
71  
72  
73 =head1 SYNOPSIS
74  
75 A typical invocation looks like this:
76 my $token = Net::Server::Framework::Crypt::encrypt( $string, $pass, 'blowfish', 'a' );
77  
78 =head1 DESCRIPTION
79  
80 This library currently supports only blowfish as encryption algorithm
81 but extending it is easy. It is used to hash passwords and en/decrypt
82 information that should be stored securely.
83  
84 =head1 BASIC METHODS
85  
86 The commands accepted by the lib are:
87  
88 =head2 encrypt
89  
90 Encrypts a string.
91  
92 =head2 decrypt
93  
94 Decrypts a string
95  
96 =head2 hash
97  
98 Hashes a string
99  
100 =head1 BUGS AND LIMITATIONS
101  
102 There are no known bugs in this module.
103 Please report problems to
104 Lenz Gschwendtner ( <lenz@springtimesoft.com> )
105 Patches are welcome.
106  
107 =head1 AUTHOR
108  
109 Lenz Gschwendtner ( <lenz@springtimesoft.com> )
110  
111  
112  
113 =head1 LICENCE AND COPYRIGHT
114  
115 Copyright (c)
116 2007 Lenz Gschwerndtner ( <lenz@springtimesoft.comn> )
117 All rights reserved.
118  
119 This module is free software; you can redistribute it and/or
120 modify it under the same terms as Perl itself. See L<perlartistic>.
121  
122 This program is distributed in the hope that it will be useful,
123 but WITHOUT ANY WARRANTY; without even the implied warranty of
124 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
« Framework/Auth.pm ↑ Summary Framework/Format.pm »