| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package PAGI::Utils::Random; |
|
2
|
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
180725
|
use strict; |
|
|
7
|
|
|
|
|
11
|
|
|
|
7
|
|
|
|
|
222
|
|
|
4
|
7
|
|
|
7
|
|
24
|
use warnings; |
|
|
7
|
|
|
|
|
9
|
|
|
|
7
|
|
|
|
|
281
|
|
|
5
|
7
|
|
|
7
|
|
25
|
use Exporter 'import'; |
|
|
7
|
|
|
|
|
8
|
|
|
|
7
|
|
|
|
|
1394
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @EXPORT_OK = qw(secure_random_bytes); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub secure_random_bytes { |
|
10
|
130
|
|
|
130
|
1
|
163868
|
my ($length) = @_; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Try /dev/urandom first (Unix) |
|
13
|
130
|
50
|
|
|
|
4129
|
if (open my $fh, '<:raw', '/dev/urandom') { |
|
14
|
130
|
|
|
|
|
231
|
my $bytes; |
|
15
|
130
|
|
|
|
|
5960
|
read($fh, $bytes, $length); |
|
16
|
130
|
|
|
|
|
954
|
close $fh; |
|
17
|
130
|
50
|
33
|
|
|
942
|
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__ |