Skip to content

Commit

Permalink
Merge pull request #4 from ap-contrib/main
Browse files Browse the repository at this point in the history
Moving $OSNAME checks to compile time
  • Loading branch information
david-dick authored Feb 16, 2024
2 parents 6814f8e + 97a7858 commit 403fb4f
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions lib/Crypt/URandom.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, );
our $VERSION = '0.39';
our @CARP_NOT = ('Crypt::URandom');

sub CRYPT_SILENT { return 64; } # hex 40
sub PROV_RSA_FULL { return 1; }
sub VERIFY_CONTEXT { return 4_026_531_840; } # hex 'F0000000'
sub W2K_MAJOR_VERSION { return 5; }
sub W2K_MINOR_VERSION { return 0; }
sub SINGLE_QUOTE { return q[']; }

sub PATH {
## no critic (ProhibitConstantPragma)
# using constant for the speed benefit of constant-folding of values

use constant CRYPT_SILENT => 64; # hex 40
use constant PROV_RSA_FULL => 1;
use constant VERIFY_CONTEXT => 4_026_531_840; # hex 'F0000000'
use constant W2K_MAJOR_VERSION => 5;
use constant W2K_MINOR_VERSION => 0;

use constant OS_WIN32 => $OSNAME eq 'MSWin32';
use constant PATH => do {
my $path = '/dev/urandom';
if ( $OSNAME eq 'freebsd' ) {
$path = '/dev/random'; # FreeBSD's /dev/random is non-blocking
}
return $path;
}
$path;
};

## use critic

my $_initialised;
my $_context;
Expand All @@ -37,7 +42,7 @@ my $_rtlgenrand;
my $_urandom_handle;

sub _init {
if ( $OSNAME eq 'MSWin32' ) {
if ( OS_WIN32() ) {
require Win32;
require Win32::API;
require Win32::API::Type;
Expand Down Expand Up @@ -100,11 +105,8 @@ _RTLGENRANDOM_PROTO_
else {
require FileHandle;
$_urandom_handle = FileHandle->new( PATH(), Fcntl::O_RDONLY() )
or Carp::croak( 'Failed to open '
. SINGLE_QUOTE()
. PATH()
. SINGLE_QUOTE()
. " for reading:$OS_ERROR" );
or Carp::croak(
q[Failed to open '] . PATH() . qq['. " for reading:$OS_ERROR] );
binmode $_urandom_handle;
}
return;
Expand Down Expand Up @@ -137,7 +139,7 @@ sub _urandom {
_init();
$_initialised = $PROCESS_ID;
}
if ( $OSNAME eq 'MSWin32' ) {
if ( OS_WIN32() ) {
my $buffer = chr(0) x $length;
if ($_cryptgenrandom) {

Expand All @@ -164,21 +166,15 @@ sub _urandom {
else {
$_urandom_handle = undef;
$_initialised = undef;
Carp::croak( "Only read $result bytes from "
. SINGLE_QUOTE()
. PATH()
. SINGLE_QUOTE() );
Carp::croak(
qq[Only read $result bytes from '] . PATH() . q['] );
}
}
else {
my $error = $OS_ERROR;
$_urandom_handle = undef;
$_initialised = undef;
Carp::croak( 'Failed to read from '
. SINGLE_QUOTE()
. PATH()
. SINGLE_QUOTE()
. ":$error" );
Carp::croak( q[Failed to read from '] . PATH() . qq[':$error] );
}
}
}
Expand Down

0 comments on commit 403fb4f

Please sign in to comment.