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             $PAGI::Utils::Random::VERSION = '0.002000';
3 7     7   141268 use strict;
  7         12  
  7         260  
4 7     7   31 use warnings;
  7         13  
  7         320  
5 7     7   26 use Exporter 'import';
  7         8  
  7         1575  
6              
7             our @EXPORT_OK = qw(secure_random_bytes);
8              
9             sub secure_random_bytes {
10 130     130 1 151809 my ($length) = @_;
11              
12             # Try /dev/urandom first (Unix)
13 130 50       4008 if (open my $fh, '<:raw', '/dev/urandom') {
14 130         201 my $bytes;
15 130         5944 read($fh, $bytes, $length);
16 130         982 close $fh;
17 130 50 33     956 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__