| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $bytes and length $bytes == $length
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
| |||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Fallback: use Crypt::URandom if available | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if (eval { require Crypt::URandom; 1 }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||