| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::SessionX::Generate::MD5; | ||
| 4 | # Generates session identifier tokens using MD5 | ||
| 5 | # Copyright (c) 2001 Gerald Richter / ecos gmbh | ||
| 6 | # Copyright (c) 2000 Jeffrey William Baker (jwbaker@acm.org) | ||
| 7 | # Distribute under the Artistic License | ||
| 8 | # | ||
| 9 | ############################################################################ | ||
| 10 | |||
| 11 | package Apache::SessionX::Generate::MD5; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use vars qw($VERSION); | ||
| 15 | use Digest::MD5 () ; | ||
| 16 | |||
| 17 | $VERSION = '2.2'; | ||
| 18 | |||
| 19 | sub generate { | ||
| 20 | my $session = shift; | ||
| 21 | my $arg = shift; | ||
| 22 | my $length = 32; | ||
| 23 | |||
| 24 | if (exists $session->{args}->{IDLength}) { | ||
| 25 | $length = $session->{args}->{IDLength}; | ||
| 26 | } | ||
| 27 | |||
| 28 | $session->{data}->{_session_id} = | ||
| 29 | substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex($arg || (time(). {}. rand(). $$))), 0, $length); | ||
| 30 | |||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | sub validate { | ||
| 35 | #This routine checks to ensure that the session ID is in the form | ||
| 36 | #we expect. This must be called before we start diddling around | ||
| 37 | #in the database or the disk. | ||
| 38 | |||
| 39 | my $session = shift; | ||
| 40 | |||
| 41 | if ($session->{data}->{_session_id} !~ /^[a-fA-F0-9]+$/) { | ||
| 42 | die 'Invalid session id' ; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; | ||
| 47 | |||
| 48 | =pod | ||
| 49 | |||
| 50 | =head1 NAME | ||
| 51 | |||
| 52 | Apache::Session::Generate::MD5 - Use MD5 to create random object IDs | ||
| 53 | |||
| 54 | =head1 SYNOPSIS | ||
| 55 | |||
| 56 | use Apache::SessionX::Generate::MD5; | ||
| 57 | |||
| 58 | $id = Apache::SessionX::Generate::MD5::generate($string); | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | This module fulfills the ID generation interface of Apache::SessionX. | ||
| 63 | If you don't give the argument C<$string>, the | ||
| 64 | IDs are generated using a two-round MD5 of a random number, the time since the | ||
| 65 | epoch, the process ID, and the address of an anonymous hash. The resultant ID | ||
| 66 | number is highly entropic on Linux and other platforms that have good | ||
| 67 | random number generators. You are encouraged to investigate the quality of | ||
| 68 | your system's random number generator if you are using the generated ID | ||
| 69 | numbers in a secure environment. | ||
| 70 | If you give C<$string> the ID is the MD5 hash of that string. | ||
| 71 | |||
| 72 | This module can also examine session IDs to ensure that they are, indeed, | ||
| 73 | session ID numbers and not evil attacks. The reader is encouraged to | ||
| 74 | consider the effect of bogus session ID numbers in a system which uses | ||
| 75 | these ID numbers to access disks and databases. | ||
| 76 | |||
| 77 | This modules takes one argument in the usual Apache::Session style. The | ||
| 78 | argument is IDLength, and the value, between 0 and 32, tells this module | ||
| 79 | where to truncate the session ID. Without this argument, the session ID will | ||
| 80 | be 32 hexadecimal characters long, equivalent to a 128-bit key. | ||
| 81 | |||
| 82 | =head1 AUTHOR | ||
| 83 | |||
| 84 | This module was written by Jeffrey William Baker <jwbaker@acm.org> and | ||
| 85 | modified by Gerald Richter <richter@dev.ecos.de>. | ||
| 86 | |||
| 87 | =head1 SEE ALSO | ||
| 88 | |||
| 89 | L<Apache::Session> |