| 1 |
package Crypt::SysRandom; |
| 2 |
$Crypt::SysRandom::VERSION = '0.007'; |
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Exporter 'import'; |
| 7 |
our @EXPORT_OK = 'random_bytes'; |
| 8 |
|
| 9 |
use Carp (); |
| 10 |
use Errno (); |
| 11 |
|
| 12 |
if (eval { require Crypt::SysRandom::XS }) { |
| 13 |
*random_bytes = \&Crypt::SysRandom::XS::random_bytes; |
| 14 |
} elsif (eval { require Win32::API }) { |
| 15 |
my $genrand = Win32::API->new('advapi32', 'INT SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength)') |
| 16 |
or die "Could not import SystemFunction036: $^E"; |
| 17 |
sub random_bytes_win32 { |
| 18 |
my ($count) = @_; |
| 19 |
return '' if $count == 0; |
| 20 |
my $buffer = chr(0) x $count; |
| 21 |
$genrand->Call($buffer, $count) or Carp::croak("Could not read random bytes"); |
| 22 |
return $buffer; |
| 23 |
} |
| 24 |
*random_bytes = \&random_bytes_win32; |
| 25 |
} elsif (-e '/dev/urandom') { |
| 26 |
open my $fh, '<:raw', '/dev/urandom' or die "Couldn't open /dev/urandom: $!"; |
| 27 |
sub random_bytes_urandom { |
| 28 |
my ($count) = @_; |
| 29 |
my ($result, $offset) = ('', 0); |
| 30 |
while ($offset < $count) { |
| 31 |
my $read = sysread $fh, $result, $count - $offset, $offset; |
| 32 |
next if not defined $read and $!{EINTR}; |
| 33 |
Carp::croak("Could not read random bytes") if not defined $read or $read == 0; |
| 34 |
$offset += $read; |
| 35 |
} |
| 36 |
return $result; |
| 37 |
} |
| 38 |
*random_bytes = \&random_bytes_urandom; |
| 39 |
} else { |
| 40 |
die "No source of randomness found"; |
| 41 |
} |
| 42 |
|
| 43 |
delete @Crypt::SysRandom::{qw(random_bytes_win32 random_bytes_urandom)}; |
| 44 |
|
| 45 |
1; |
| 46 |
|
| 47 |
# ABSTRACT: Perl interface to system randomness |
| 48 |
|
| 49 |
__END__ |
| 50 |
|
| 51 |
=pod |
| 52 |
|
| 53 |
=encoding UTF-8 |
| 54 |
|
| 55 |
=head1 NAME |
| 56 |
|
| 57 |
Crypt::SysRandom - Perl interface to system randomness |
| 58 |
|
| 59 |
=head1 VERSION |
| 60 |
|
| 61 |
version 0.007 |
| 62 |
|
| 63 |
=head1 SYNOPSIS |
| 64 |
|
| 65 |
use Crypt::SysRandom 'random_bytes'; |
| 66 |
my $random = random_bytes(16); |
| 67 |
|
| 68 |
=head1 DESCRIPTION |
| 69 |
|
| 70 |
This module uses whatever interface is available to procure cryptographically random data from the system. |
| 71 |
|
| 72 |
=head1 FUNCTIONS |
| 73 |
|
| 74 |
=head2 random_bytes($count) |
| 75 |
|
| 76 |
This will fetch a string of C<$count> random bytes containing cryptographically secure random date. |
| 77 |
|
| 78 |
=head1 Backends |
| 79 |
|
| 80 |
The current backends are tried in order: |
| 81 |
|
| 82 |
=over 4 |
| 83 |
|
| 84 |
=item * L<Crypt::SysRandom::XS|Crypt::SysRandom::XS> |
| 85 |
|
| 86 |
=item * C<RtlGenRandom> using L<Win32::API|Win32::API> |
| 87 |
|
| 88 |
=item * C</dev/urandom> |
| 89 |
|
| 90 |
=back |
| 91 |
|
| 92 |
=head1 AUTHOR |
| 93 |
|
| 94 |
Leon Timmermans <[email protected]> |
| 95 |
|
| 96 |
=head1 COPYRIGHT AND LICENSE |
| 97 |
|
| 98 |
This software is copyright (c) 2025 by Leon Timmermans. |
| 99 |
|
| 100 |
This is free software; you can redistribute it and/or modify it under |
| 101 |
the same terms as the Perl 5 programming language system itself. |
| 102 |
|
| 103 |
=cut |