Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session/Generate/SHA256.pm

raw
1 #############################################################################
2 #
3 # Lemonldap::NG::Common::Apache::Session::Generate::SHA256
4 # Generates session identifier tokens using SHA-256
5 # Distribute under the Perl License
6 #
7 ############################################################################
8
9 package Lemonldap::NG::Common::Apache::Session::Generate::SHA256;
10
11 use strict;
12 use Crypt::URandom;
13
14 our $VERSION = '2.0.2';
15
16 sub generate {
17 my $session = shift;
18 my $length = 64;
19
20 if ( exists $session->{args}->{IDLength} ) {
21 $length = $session->{args}->{IDLength};
22 }
23
24 eval {
25 $session->{data}->{_session_id} =
26 unpack( 'H*', Crypt::URandom::urandom( int( $length / 2 ) ) );
27 };
28 if ($@) {
29 print STDERR "Crypt::URandom::urandom failed: $@\n";
30 require Digest::SHA;
31 $session->{data}->{_session_id} =
32 substr( Digest::SHA::sha256_hex( time() . {} . rand() . $$ ),
33 0, $length );
34 }
35 }
36
37 sub validate {
38
39 #This routine checks to ensure that the session ID is in the form
40 #we expect. This must be called before we start diddling around
41 #in the database or the disk.
42
43 my $session = shift;
44
45 if ( $session->{data}->{_session_id} =~ /^([a-fA-F0-9]+)$/ ) {
46 $session->{data}->{_session_id} = $1;
47 }
48 else {
49 die "Invalid session ID: " . $session->{data}->{_session_id};
50 }
51 }
52
53 1;