File Coverage

blib/lib/PAGI/Utils/Random.pm
Criterion Covered Total %
statement 15 20 75.0
branch 2 6 33.3
condition 1 3 33.3
subroutine 4 4 100.0
pod 1 1 100.0
total 23 34 67.6


line stmt bran cond sub pod time code
1             package PAGI::Utils::Random;
2              
3 7     7   143148 use strict;
  7         8  
  7         236  
4 7     7   29 use warnings;
  7         27  
  7         337  
5 7     7   27 use Exporter 'import';
  7         12  
  7         1460  
6              
7             our @EXPORT_OK = qw(secure_random_bytes);
8              
9             sub secure_random_bytes {
10 130     130 1 162626 my ($length) = @_;
11              
12             # Try /dev/urandom first (Unix)
13 130 50       3921 if (open my $fh, '<:raw', '/dev/urandom') {
14 130         233 my $bytes;
15 130         5772 read($fh, $bytes, $length);
16 130         950 close $fh;
17 130 50 33     946 return $bytes if defined $bytes && length($bytes) == $length;
18             }
19              
20             # Fallback: use Crypt::URandom if available
21 0 0         if (eval { require Crypt::URandom; 1 }) {
  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             1;
29              
30             __END__