| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package HTTP::Session2::Random; | 
| 2 | 12 |  |  | 12 |  | 21418 | use strict; | 
|  | 12 |  |  |  |  | 25 |  | 
|  | 12 |  |  |  |  | 592 |  | 
| 3 | 12 |  |  | 12 |  | 66 | use warnings; | 
|  | 12 |  |  |  |  | 22 |  | 
|  | 12 |  |  |  |  | 350 |  | 
| 4 | 12 |  |  | 12 |  | 1152 | use utf8; | 
|  | 12 |  |  |  |  | 27 |  | 
|  | 12 |  |  |  |  | 86 |  | 
| 5 | 12 |  |  | 12 |  | 515 | use 5.008_001; | 
|  | 12 |  |  |  |  | 39 |  | 
|  | 12 |  |  |  |  | 635 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | # DO NOT USE THIS DIRECTLY. | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 12 |  |  | 12 |  | 2979 | use MIME::Base64 (); | 
|  | 12 |  |  |  |  | 6780 |  | 
|  | 12 |  |  |  |  | 211 |  | 
| 10 | 12 |  |  | 12 |  | 5771 | use Digest::SHA (); | 
|  | 12 |  |  |  |  | 24180 |  | 
|  | 12 |  |  |  |  | 264 |  | 
| 11 | 12 |  |  | 12 |  | 13491 | use Time::HiRes; | 
|  | 12 |  |  |  |  | 24090 |  | 
|  | 12 |  |  |  |  | 63 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $URANDOM_FH; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # $URANDOM_FH is undef if there is no /dev/urandom | 
| 16 |  |  |  |  |  |  | open $URANDOM_FH, '<:raw', '/dev/urandom' | 
| 17 |  |  |  |  |  |  | or do { | 
| 18 |  |  |  |  |  |  | undef $URANDOM_FH; | 
| 19 |  |  |  |  |  |  | warn "Cannot open /dev/urandom: $!."; | 
| 20 |  |  |  |  |  |  | }; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub generate_session_id { | 
| 23 | 27 | 100 |  | 27 | 0 | 1950 | if ($URANDOM_FH) { | 
| 24 | 25 |  |  |  |  | 47 | my $length = 24; | 
| 25 |  |  |  |  |  |  | # Generate session id from /dev/urandom. | 
| 26 | 25 |  |  |  |  | 10574 | my $read = read($URANDOM_FH, my $buf, $length); | 
| 27 | 25 | 50 |  |  |  | 108 | if ($read != $length) { | 
| 28 | 0 |  |  |  |  | 0 | die "Cannot read bytes from /dev/urandom: $!"; | 
| 29 |  |  |  |  |  |  | } | 
| 30 | 25 |  |  |  |  | 221 | my $result = MIME::Base64::encode_base64($buf, ''); | 
| 31 | 25 |  |  |  |  | 91 | $result =~ tr|+/=|\-_|d; # make it url safe | 
| 32 | 25 |  |  |  |  | 155 | return substr($result, 0, 31); | 
| 33 |  |  |  |  |  |  | } else { | 
| 34 |  |  |  |  |  |  | # It's weaker than above. But it's portable. | 
| 35 | 2 |  |  |  |  | 124 | substr(Digest::SHA::sha1_hex(rand() . $$ . {} . Time::HiRes::time()),int(rand(4)),31); | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | 1; |