File Coverage

blib/lib/LibWeb/Digest.pm
Criterion Covered Total %
statement 10 30 33.3
branch 0 12 0.0
condition 1 3 33.3
subroutine 3 6 50.0
pod n/a
total 14 51 27.4


line stmt bran cond sub pod time code
1             #==============================================================================
2             # LibWeb::Digest -- Digest generation for libweb applications.
3              
4             package LibWeb::Digest;
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: Digest.pm,v 1.2 2000/07/18 06:33:30 ckyc Exp $
24              
25             #-##############################
26             # Use standard library.
27 1     1   2485 use strict;
  1         3  
  1         51  
28 1     1   6 use vars qw($VERSION @ISA);
  1         3  
  1         416  
29             require Digest::HMAC;
30             require Digest::SHA1;
31             require Digest::MD5;
32              
33             #-##############################
34             # Use custom library.
35             require LibWeb::Class;
36              
37             #-##############################
38             # Version.
39             $VERSION = '0.02';
40              
41             #-##############################
42             # Inheritance.
43             @ISA = qw(LibWeb::Class);
44              
45             #-##############################
46             # Methods.
47             sub new {
48 1     1   28 my ($class, $Class, $self);
49 1         3 $class = shift;
50 1   33     8 $Class = ref($class) || $class;
51 1         18 $self = $Class->SUPER::new(shift);
52 0           bless($self, $Class);
53             }
54              
55 0     0     sub DESTROY {}
56              
57             sub generate_MAC {
58             #
59             # Params: -data=>, -key=>, -algorithm=>, -format=>
60             # e.g. -algorithm => 'Digest::MD5'
61             # e.g. -algorithm => 'Digest::SHA1'
62             # e.g. -format => 'binary' or 'hex' or 'b64'.
63             #
64 0     0     my ($self, $data, $key, $algorithm, $format, $hmac);
65 0           $self = shift;
66 0           ($data, $key, $algorithm, $format) =
67             $self->rearrange(['DATA', 'KEY', 'ALGORITHM', 'FORMAT'], @_);
68              
69 0           $format = uc($format);
70 0           $hmac = Digest::HMAC->new($key, $algorithm);
71 0           $hmac->add($data);
72 0 0         return $hmac->digest if ($format eq 'BINARY');
73 0 0         return $hmac->hexdigest if ($format eq 'HEX');
74 0 0         return $hmac->b64digest if ($format eq 'B64');
75             }
76              
77             sub generate_digest {
78             #
79             # Params: -data=>, -key=>, -algorithm=>, -format=>
80             # e.g. -algorithm => 'Digest::MD5'
81             # e.g. -algorithm => 'Digest::SHA1'
82             # e.g. -format => 'binary' or 'hex' or 'b64'.
83             #
84 0     0     my ($self, $data, $key, $algorithm, $format, $ctx);
85 0           $self = shift;
86 0           ($data, $key, $algorithm, $format) =
87             $self->rearrange(['DATA', 'KEY', 'ALGORITHM', 'FORMAT'], @_);
88              
89 0           $format = uc($format);
90 0           $ctx = $algorithm->new;
91 0           $ctx->add( $data . $key );
92 0 0         return $ctx->digest if ($format eq 'BINARY');
93 0 0         return $ctx->hexdigest if ($format eq 'HEX');
94 0 0         return $ctx->b64digest if ($format eq 'B64');
95             }
96              
97             1;
98             __END__