| 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 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.9
| |||||||||||||||||||||
| 48 | 1 | my ($class, $Class, $self); | |||||||||||||||||||
| 49 | 1 | $class = shift; | |||||||||||||||||||
| 50 | 1 | ▶ | $Class = ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 51 | 1 | $self = $Class->SUPER::new(shift); | |||||||||||||||||||
| 52 | 0 | bless($self, $Class); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | sub generate_MAC { | ||||||||||||||||||
|
Subroutine
generate_MAC: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 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 | 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 | ▶ | return $hmac->digest if ($format eq 'BINARY'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | ▶ | return $hmac->hexdigest if ($format eq 'HEX'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | ▶ | return $hmac->b64digest if ($format eq 'B64'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | ▶ | sub generate_digest { | ||||||||||||||||||
|
Subroutine
generate_digest: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 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 | 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 | ▶ | return $ctx->digest if ($format eq 'BINARY'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | ▶ | return $ctx->hexdigest if ($format eq 'HEX'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | ▶ | return $ctx->b64digest if ($format eq 'B64'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1; | ||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||