1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
=head1 NAME
Data::Entropy::RawSource::RandomOrg - download entropy from random.org
=head1 SYNOPSIS
use Data::Entropy::RawSource::RandomOrg;
my $rawsrc = Data::Entropy::RawSource::RandomOrg->new;
$c = $rawsrc->getc;
# and the rest of the I/O handle interface
=head1 DESCRIPTION
This class provides an I/O handle connected to a stream of random octets
being generated by an electromagnetic noise detector connected to the
random.org server. This is a strong source of random bits, but is not
suitable for security applications because the bits are passed over the
Internet unencrypted. The handle implements a substantial subset of
the interface described in L<IO::Handle>.
For use as a general entropy source, it is recommended to wrap an object
of this class using C<Data::Entropy::Source>, which provides methods to
extract entropy in more convenient forms than mere octets.
The bits generated at random.org are, theoretically and as far as anyone
can tell, totally unbiased and uncorrelated. However, they are sent
over the Internet in the clear, and so are subject to interception and
alteration by an adversary. This is therefore generally unsuitable for
security applications. The capacity of the random bit server is also
limited. This class will slow down requests if the server's entropy
pool is less than half full, and (as requested by the server operators)
pause entirely if the entropy pool is less than 20% full.
Applications requiring secret entropy should generate it locally
(see L<Data::Entropy::RawSource::Local>). Applications requiring a
large amount of entropy should generate it locally or download it from
randomnumbers.info (see L<Data::Entropy::RawSource::RandomnumbersInfo>).
Applications requiring a large amount of apparently-random data,
but not true entropy, might prefer to fake it cryptographically (see
L<Data::Entropy::RawSource::CryptCounter>).
=cut
package Data::Entropy::RawSource::RandomOrg;
{ use 5.006; }
use warnings;
use strict;
use Errno 1.00 qw(EIO);
use HTTP::Lite 2.2 ();
our $VERSION = "0.008";
=head1 CONSTRUCTOR
=over
=item Data::Entropy::RawSource::RandomOrg->new
Creates and returns a handle object referring to a stream of random
octets generated by random.org.
=cut
sub new {
my($class) = @_;
my $http = HTTP::Lite->new;
$http->http11_mode(1);
return bless({
http => $http,
buffer => "",
bufpos => 0,
error => 0,
}, $class);
}
=back
=head1 METHODS
A subset of the interfaces described in L<IO::Handle> and L<IO::Seekable>
are provided:
=over
=item $rawsrc->read(BUFFER, LENGTH[, OFFSET])
=item $rawsrc->getc
=item $rawsrc->ungetc(ORD)
=item $rawsrc->eof
Buffered reading from the source, as in L<IO::Handle>.
=item $rawsrc->sysread(BUFFER, LENGTH[, OFFSET])
Unbuffered reading from the source, as in L<IO::Handle>.
=item $rawsrc->close
Does nothing.
=item $rawsrc->opened
Retruns true to indicate that the source is available for I/O.
=item $rawsrc->clearerr
=item $rawsrc->error
Error handling, as in L<IO::Handle>.
=back
The buffered (C<read> et al) and unbuffered (C<sysread> et al) sets
of methods are interchangeable, because no such distinction is made by
this class.
Methods to write to the file are unimplemented because the stream is
fundamentally read-only. Methods to seek are unimplemented because the
stream is non-rewindable; C<ungetc> works, however.
=cut
sub _checkbuf {
my($self) = @_;
$self->{http}->reset;
unless($self->{http}->request(
"http://www.random.org/cgi-bin/checkbuf"
) == 200) {
$! = EIO;
return undef;
}
unless($self->{http}->body =~
/\A[\ \t\n]*([0-9]{1,3}(?:\.[0-9]+)?)\%[\ \t\n]*\z/) {
$! = EIO;
return undef;
}
return $1;
}
sub _ensure_buffer {
my($self) = @_;
return 1 unless $self->{bufpos} == length($self->{buffer});
while(1) {
my $fillpct = $self->_checkbuf;
return 0 unless defined $fillpct;
if($fillpct >= 20) {
sleep((50 - $fillpct)*0.2) if $fillpct < 50;
last;
}
sleep 10;
}
$self->{http}->reset;
unless($self->{http}->request(
"http://www.random.org/cgi-bin/randbyte?nbytes=256&format=f"
) == 200) {
$! = EIO;
return 0;
}
$self->{buffer} = $self->{http}->body;
$self->{bufpos} = 0;
if($self->{buffer} !~ /\A[\x00-\xff]+\z/) {
$self->{buffer} = "";
$! = EIO;
return 0;
}
return 1;
}
sub close { 1 }
sub opened { 1 }
sub error { $_[0]->{error} }
sub clearerr {
my($self) = @_;
$self->{error} = 0;
return 0;
}
sub getc {
my($self) = @_;
unless($self->_ensure_buffer) {
$self->{error} = 1;
return undef;
}
return substr($self->{buffer}, $self->{bufpos}++, 1);
}
sub ungetc {
my($self, $cval) = @_;
if($self->{bufpos} == 0) {
$self->{buffer} = chr($cval).$self->{buffer};
} else {
$self->{bufpos}--;
}
}
sub read {
my($self, undef, $length, $offset) = @_;
return undef if $length < 0;
$_[1] = "" unless defined $_[1];
if(!defined($offset)) {
$offset = 0;
$_[1] = "";
} elsif($offset < 0) {
return undef if $offset < -length($_[1]);
substr $_[1], $offset, -$offset, "";
$offset = length($_[1]);
} elsif($offset > length($_[1])) {
$_[1] .= "\0" x ($offset - length($_[1]));
} else {
substr $_[1], $offset, length($_[1]) - $offset, "";
}
my $original_offset = $offset;
while($length != 0) {
unless($self->_ensure_buffer) {
$self->{error} = 1;
last;
}
my $avail = length($self->{buffer}) - $self->{bufpos};
if($length < $avail) {
$_[1] .= substr($self->{buffer}, $self->{bufpos},
$length);
$offset += $length;
$self->{bufpos} += $length;
last;
}
$_[1] .= substr($self->{buffer}, $self->{bufpos}, $avail);
$offset += $avail;
$length -= $avail;
$self->{bufpos} += $avail;
}
my $nread = $offset - $original_offset;
return $nread == 0 ? undef : $nread;
}
*sysread = \&read;
sub eof { 0 }
=head1 SEE ALSO
L<Data::Entropy::RawSource::CryptCounter>,
L<Data::Entropy::RawSource::Local>,
L<Data::Entropy::RawSource::RandomnumbersInfo>,
L<Data::Entropy::Source>,
L<http://www.random.org>
=head1 AUTHOR
Andrew Main (Zefram) <zefram@fysh.org>
Maintained by Robert Rothenberg <rrwo@cpan.org>
=head1 COPYRIGHT
Copyright (C) 2006, 2007, 2009, 2011, 2025
Andrew Main (Zefram) <zefram@fysh.org>
=head1 LICENSE
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|