use strict; use warnings; use HotBits; my $x= new HotBits::; my $bits= $x->request (16); # parameter is bytes to fetch, up to 2048. print length($bits)," bytes: "; print unpack("H*", $bits), "\n"; #### =head1 NAME B - download hardware random numbers =head1 AUTHOR John M. Dlugosz - john@dlugosz.com - http://www.dlugosz.com =head1 DESCRIPTION This module will access "genuine" random numbers via http://www.fourmilab.ch/hotbits/. This server at Fourmilab in Switzerland uses radioactive decay to offer the truest random number source possible. This module works the same as the site's Java class for the same purpose: access the CGI program via HTTP. =cut package HotBits; use strict; use warnings; our $VERSION= v1.0; require LWP::UserAgent; use Carp; my $serverURL='http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits'; sub new { my $class= shift; my $URL= shift || $serverURL; # optional argument my $ua = new LWP::UserAgent::; $ua->env_proxy(); my $self= bless { ua => $ua, URL => $URL }, $class; return $self; } sub request { my ($self, $count)= @_; $count ||= 128; my $request = HTTP::Request->new('GET', "$self->{URL}?nbytes=$count&fmt=bin"); $request->proxy_authorization_basic($ENV{HTTP_proxy_user}, $ENV{HTTP_proxy_pass}) if $ENV{HTTP_proxy_user}; my $response = $self->{ua}->request($request); croak "Error from $self->{URL}", if ($response->code() != 200); croak "Unexpected return (not binary) from $self->{URL}" if $response->headers()->content_type() ne "application/octet-stream"; return $response->content(); } 1; # module loaded OK.