| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | Data::Entropy::RawSource::Local - read randomness from local device | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use Data::Entropy::RawSource::Local; | ||||||||||
| 8 | |||||||||||
| 9 | my $rawsrc = Data::Entropy::RawSource::Local->new; | ||||||||||
| 10 | |||||||||||
| 11 | $rawsrc->sysread($c, 1); | ||||||||||
| 12 | # and the rest of the I/O handle interface | ||||||||||
| 13 | |||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||
| 15 | |||||||||||
| 16 | This class provides a constructor to open an I/O handle connected to | ||||||||||
| 17 | a local source of random octets. This may be a strong entropy source, | ||||||||||
| 18 | depending on the OS, but not every OS has such a facility at all. | ||||||||||
| 19 | |||||||||||
| 20 | There are no actual objects blessed into this class. Only the constructor | ||||||||||
| 21 | belongs to this class; it returns C<IO::File> objects. For use as | ||||||||||
| 22 | a general entropy source, it is recommended to wrap the handle using | ||||||||||
| 23 | C<Data::Entropy::Source>, which provides methods to extract entropy in | ||||||||||
| 24 | more convenient forms than mere octets. | ||||||||||
| 25 | |||||||||||
| 26 | On systems with a blocking B</dev/random>, such as Linux, the bits | ||||||||||
| 27 | generated can be totally unbiased and uncorrelated. Such an entropy | ||||||||||
| 28 | stream is suitable for all uses, including security applications. | ||||||||||
| 29 | However, the rate of entropy generation is limited, so applications | ||||||||||
| 30 | requiring a large amount of apparently-random data might prefer to fake | ||||||||||
| 31 | it cryptographically (see L<Data::Entropy::RawSource::CryptCounter>). | ||||||||||
| 32 | |||||||||||
| 33 | On systems where B</dev/random> does not block, the bits generated are | ||||||||||
| 34 | necessarily correlated to some extent, but it should be cryptographically | ||||||||||
| 35 | difficult to detect the correlation. Such an entropy source is not | ||||||||||
| 36 | suitable for all applications. Some other systems lack B</dev/random> | ||||||||||
| 37 | entirely. | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | package Data::Entropy::RawSource::Local; | ||||||||||
| 42 | |||||||||||
| 43 | 2 | ▶ | { use 5.006; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 2 | ▶ | use IO::File 1.03; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | 2 | our $VERSION = "0.010"; | |||||||||
| 51 | |||||||||||
| 52 | =head1 CONSTRUCTOR | ||||||||||
| 53 | |||||||||||
| 54 | =over | ||||||||||
| 55 | |||||||||||
| 56 | =item Data::Entropy::RawSource::Local->new([FILENAME]) | ||||||||||
| 57 | |||||||||||
| 58 | Opens a file handle referring to the randomness device, or C<die>s | ||||||||||
| 59 | on error. The device opened is B</dev/random> by default, but this may | ||||||||||
| 60 | be overridden by giving a FILENAME argument. | ||||||||||
| 61 | |||||||||||
| 62 | The default device name may in the future be different on different OSes, | ||||||||||
| 63 | if their equivalent devices are in different places. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 68 | 2 | my($class, $filename) = @_; | |||||||||
| 69 | 2 | ▶ | $filename = "/dev/random" unless defined $filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 2 | my $self = IO::File->new($filename, "r"); | |||||||||
| 71 | 2 | ▶ | croak "can't open $filename: $!" unless defined $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | return $self; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =back | ||||||||||
| 76 | |||||||||||
| 77 | =head1 METHODS | ||||||||||
| 78 | |||||||||||
| 79 | There are no actual objects blessed into this class. The constuctor | ||||||||||
| 80 | returns C<IO::File> objects. See L<IO::File> for the interface. It is | ||||||||||
| 81 | recommended to use unbuffered reads (the C<sysread> method) rather than | ||||||||||
| 82 | buffered reads (the C<getc> method et al), to avoid wasting entropy that | ||||||||||
| 83 | could be used by another process. | ||||||||||
| 84 | |||||||||||
| 85 | =head1 SEE ALSO | ||||||||||
| 86 | |||||||||||
| 87 | L<Data::Entropy::RawSource::CryptCounter>, | ||||||||||
| 88 | L<Data::Entropy::RawSource::RandomOrg>, | ||||||||||
| 89 | L<Data::Entropy::RawSource::RandomnumbersInfo>, | ||||||||||
| 90 | L<Data::Entropy::Source>, | ||||||||||
| 91 | L<IO::File> | ||||||||||
| 92 | |||||||||||
| 93 | =head1 AUTHOR | ||||||||||
| 94 | |||||||||||
| 95 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 96 | |||||||||||
| 97 | Maintained by Robert Rothenberg <perl@rhizomnic.com> | ||||||||||
| 98 | |||||||||||
| 99 | =head1 COPYRIGHT | ||||||||||
| 100 | |||||||||||
| 101 | Copyright (C) 2006, 2007, 2009, 2011, 2025-2026 | ||||||||||
| 102 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 103 | |||||||||||
| 104 | =head1 LICENSE | ||||||||||
| 105 | |||||||||||
| 106 | This module is free software; you can redistribute it and/or modify it | ||||||||||
| 107 | under the same terms as Perl itself. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 2 | 1; | |||||||||