blib/lib/PAGI/Utils/Random.pm

filter: Statementstmt 78.2% 18 / 23 Branchbran 33.3% 2 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 66.6% 26 / 39 CC 5 SCAR 18.1
CC
5
Cov
66%
secure_random_bytes
9.2
BEGIN
1.0
BEGIN
1.0
CRAP 6.1
« blib/lib/PAGI/Request/Upload.pm ↑ Summary blib/lib/PAGI/Context.pm »
1 package PAGI::Utils::Random;
2 7 $PAGI::Utils::Random::VERSION = '0.002002';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our @EXPORT_OK = qw(secure_random_bytes);
8  
9 145 ▶sub secure_random_bytes {
Subroutine secure_random_bytes: called CC=5 SCAR=22.1
Pod documented
10 145 my ($length) = @_;
11  
12 # Try /dev/urandom first (Unix)
13 145 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 145 0 145
14 145 my $bytes;
15 145 read($fh, $bytes, $length);
16 145 close $fh;
17 145 ▶ return $bytes if defined $bytes && length($bytes) == $length;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bytes and length $bytes == $length 145 0 145
Condition33%1/3
defined $bytes and length $bytes == $length
!ll&&!rl&&r
00145
MC/DC0%0/2
defined $bytes and length $bytes == $length
defined $bytes length $bytes == $length
Truth table33%1/3
A and B
A= defined $bytesB= length $bytes == $length
ABresult
0X0
100
111
18 }
19  
20 # Fallback: use Crypt::URandom if available
21 0 ▶ if (eval { require Crypt::URandom; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Crypt::URandom; 1 } }) 0 0 0
22 0 return Crypt::URandom::urandom($length);
23 }
24  
25 0 die "No secure random source available (need /dev/urandom or Crypt::URandom)\n";
26 }
27  
28 7 1;
29  
30 __END__
« blib/lib/PAGI/Request/Upload.pm ↑ Summary blib/lib/PAGI/Context.pm »