| 1 | package HTTP::Session::ID::Urandom; | ||||||||||||||||||||||||||||
| 2 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Crypt::URandom (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 52 | ▶ | sub generate_id { | ||||||||||||||||||||||||||
|
Subroutine
generate_id: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 52 | my ($class, $sid_length) = @_; | |||||||||||||||||||||||||||
| 13 | 52 | ▶ | if ( !defined($sid_length) || $sid_length !~ /\A[1-9][0-9]*\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $sid_length and $sid_length =~ /\A[1-9][0-9]*\z/
MC/DC0%0/2
defined $sid_length and $sid_length =~ /\A[1-9][0-9]*\z/
defined $sid_length
$sid_length =~ /\A[1-9][0-9]*\z/
Truth table33%1/3
A and B
A= defined $sid_lengthB= $sid_length =~ /\A[1-9][0-9]*\z/
| |||||||||||||||||||||||||||||
| 14 | 0 | Carp::croak "sid_length must be a positive integer"; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | # bytes of CSPRNG input needed to yield at least $sid_length base64url chars | ||||||||||||||||||||||||||||
| 17 | 52 | my $src_len = POSIX::ceil($sid_length * 3.0 / 4.0); | |||||||||||||||||||||||||||
| 18 | # Generate session id from a cryptographically secure source. | ||||||||||||||||||||||||||||
| 19 | 52 | my $buf = Crypt::URandom::urandom($src_len); | |||||||||||||||||||||||||||
| 20 | 52 | my $result = MIME::Base64::encode_base64($buf, ''); | |||||||||||||||||||||||||||
| 21 | 52 | $result =~ tr|+/=|\-_|d; # make it url safe | |||||||||||||||||||||||||||
| 22 | 52 | return substr($result, 0, $sid_length); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 19 | 1; | |||||||||||||||||||||||||||