File Coverage

blib/lib/Apache/Session/Generate/SHA256.pm
Criterion Covered Total %
statement 16 18 88.8
branch 2 4 50.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 23 29 79.3


line stmt bran cond sub pod time code
1             # Directly copied from LemonLDAP::NG project (http://lemonldap-ng.org/)
2             package Apache::Session::Generate::SHA256;
3              
4 5     5   39 use strict;
  5         27  
  5         174  
5 5     5   39 use vars qw($VERSION);
  5         12  
  5         648  
6 5     5   2894 use Digest::SHA qw(sha256 sha256_hex sha256_base64);
  5         16710  
  5         1609  
7              
8             $VERSION = '1.2.2';
9              
10             sub generate {
11 57     57 0 617 my $session = shift;
12 57         115 my $length = 64;
13              
14 57 50       191 if ( exists $session->{args}->{IDLength} ) {
15 0         0 $length = $session->{args}->{IDLength};
16             }
17              
18 57         1361 $session->{data}->{_session_id} = substr(
19             Digest::SHA::sha256_hex(
20             Digest::SHA::sha256_hex( time() . {} . rand() . $$ )
21             ),
22             0, $length
23             );
24              
25             }
26              
27             sub validate {
28              
29             #This routine checks to ensure that the session ID is in the form
30             #we expect. This must be called before we start diddling around
31             #in the database or the disk.
32              
33 1     1 0 12 my $session = shift;
34              
35 1 50       14 if ( $session->{data}->{_session_id} =~ /^([a-fA-F0-9]+)$/ ) {
36 1         9 $session->{data}->{_session_id} = $1;
37             }
38             else {
39 0           die "Invalid session ID: " . $session->{data}->{_session_id};
40             }
41             }
42              
43             1;