blib/lib/Crypt/SysRandom.pm

filter: Statementstmt 87.1% 34 / 39 Branchbran 16.6% 1 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 83.0% 49 / 59 CC 1 SCAR 0.0
CC
1
Cov
78%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Crypt::SysRandom;
2 2 $Crypt::SysRandom::VERSION = '0.008';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our @EXPORT_OK = qw/random_bytes random_hex random_base64 random_base64url random_uuid/;
8 2 our %EXPORT_TAGS = (all => \@EXPORT_OK);
9  
10 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MIME::Base64 3.11 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 my $rtl_gen_random = 'INT SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength)';
15  
16 2 ▶if (eval { require Crypt::SysRandom::XS }) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { require Crypt::SysRandom::XS } }) { } 2 0 2
elsif (my $genrand = eval { do { require Win32::API; 'Win32::API'->new('advapi32', $rtl_gen_random) } }) { } 0 0 0
elsif (open my $fh, '<:raw', '/dev/urandom') { } 0 0 0
17 2 *random_bytes = \&Crypt::SysRandom::XS::random_bytes;
18 0 } elsif (my $genrand = eval { require Win32::API; Win32::API->new('advapi32', $rtl_gen_random) }) {
19 sub random_bytes_win32 {
20 my ($count) = @_;
21 return '' if $count == 0;
22 my $buffer = chr(0) x $count;
23 $genrand->Call($buffer, $count) or Carp::croak("Could not read random bytes");
24 return $buffer;
25 }
26 0 *random_bytes = \&random_bytes_win32;
27 } elsif (open my $fh, '<:raw', '/dev/urandom') {
28 sub random_bytes_urandom {
29 my ($count) = @_;
30 my ($result, $offset) = ('', 0);
31 while ($offset < $count) {
32 my $read = sysread $fh, $result, $count - $offset, $offset;
33 next if not defined $read and $!{EINTR};
34 Carp::croak("Could not read random bytes") if not defined $read or $read == 0;
35 $offset += $read;
36 }
37 return $result;
38 }
39 0 *random_bytes = \&random_bytes_urandom;
40 } else {
41 0 die "No source of randomness found, consider installing Crypt::SysRandom::XS";
42 }
43  
44 2 delete @Crypt::SysRandom::{qw(random_bytes_win32 random_bytes_urandom)};
45  
46 1 ▶sub random_hex {
Subroutine random_hex: called CC=1 SCAR=0.0
Pod documented
47 1 return unpack 'H*', random_bytes(@_);
48 }
49  
50 1 ▶sub random_base64 {
Subroutine random_base64: called CC=1 SCAR=0.0
Pod documented
51 1 return MIME::Base64::encode_base64(random_bytes(@_), '');
52 }
53  
54 1 ▶sub random_base64url {
Subroutine random_base64url: called CC=1 SCAR=0.0
Pod documented
55 1 return MIME::Base64::encode_base64url(random_bytes(@_));
56 }
57  
58 1 ▶sub random_uuid {
Subroutine random_uuid: called CC=1 SCAR=0.0
Pod documented
59 1 my $random = random_bytes(16);
60 1 vec($random, 35, 2) = 2; # Variant marker, must be 2 per RFC 9562
61 1 vec($random, 13, 4) = 4; # Version marker, we're doing UUIDv4
62 1 return join '-', unpack 'H8H4H4H4H12', $random;
63 }
64  
65 2 1;
66  
67 # ABSTRACT: Perl interface to system randomness
68  
69 __END__
↑ Summary