File Coverage

blib/lib/Data/Entropy/RawSource/Local.pm
Criterion Covered Total %
statement 20 20 100.0
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 30 31 96.7


line stmt bran cond sub pod time code
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 objects. For use as
22             a general entropy source, it is recommended to wrap the handle using
23             C, which provides methods to extract entropy in
24             more convenient forms than mere octets.
25              
26             On systems with a blocking B, 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).
32              
33             On systems where B 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
37             entirely. If satisfactory entropy cannot be generated locally, consider
38             downloading it from a server (see L
39             and L).
40              
41             =cut
42              
43             package Data::Entropy::RawSource::Local;
44              
45 1     1   22910 { use 5.006; }
  1         3  
  1         34  
46 1     1   4 use warnings;
  1         1  
  1         30  
47 1     1   4 use strict;
  1         2  
  1         58  
48              
49 1     1   6 use Carp qw(croak);
  1         2  
  1         101  
50 1     1   1046 use IO::File 1.03;
  1         11683  
  1         316  
51              
52             our $VERSION = "0.007";
53              
54             =head1 CONSTRUCTOR
55              
56             =over
57              
58             =item Data::Entropy::RawSource::Local->new([FILENAME])
59              
60             Opens a file handle referring to the randomness device, or Cs
61             on error. The device opened is B by default, but this may
62             be overridden by giving a FILENAME argument.
63              
64             The default device name may in the future be different on different OSes,
65             if their equivalent devices are in different places.
66              
67             =cut
68              
69             sub new {
70 2     2 1 1533 my($class, $filename) = @_;
71 2 50       9 $filename = "/dev/random" unless defined $filename;
72 2         15 my $self = IO::File->new($filename, "r");
73 2 100       427 croak "can't open $filename: $!" unless defined $self;
74 1         3 return $self;
75             }
76              
77             =back
78              
79             =head1 METHODS
80              
81             There are no actual objects blessed into this class. The constuctor
82             returns C objects. See L for the interface. It is
83             recommended to use unbuffered reads (the C method) rather than
84             buffered reads (the C method et al), to avoid wasting entropy that
85             could be used by another process.
86              
87             =head1 SEE ALSO
88              
89             L,
90             L,
91             L,
92             L,
93             L
94              
95             =head1 AUTHOR
96              
97             Andrew Main (Zefram)
98              
99             =head1 COPYRIGHT
100              
101             Copyright (C) 2006, 2007, 2009, 2011
102             Andrew Main (Zefram)
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             1;