| 1 | package NIST::Beacon; | ||
| 2 | |||
| 3 | # ABSTRACT: A Perl module for querying NIST's randomness beacon. | ||
| 4 | |||
| 5 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = "1.000"; | ||
| 9 | |||
| 10 | 4 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 4 | ▶ | use XML::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use LWP::UserAgent; | ||
| 13 | use NIST::Beacon::Record; | ||
| 14 | |||
| 15 | has _agent => (is => 'ro', default => sub { LWP::UserAgent->new(agent => 'NIST-Beacon') }); | ||
| 16 | has _url => (is => 'ro', default => sub { 'https://beacon.nist.gov/rest/record/' }); | ||
| 17 | |||
| 18 | sub latest_record { | ||
| 19 | my $self = shift; | ||
| 20 | my $url = $self->_url . 'last'; | ||
| 21 | my $response = $self->_agent->get($url); | ||
| 22 | |||
| 23 | if ($response->is_success) { | ||
| 24 | my $data = XMLin($response->decoded_content); | ||
| 25 | return NIST::Beacon::Record->new($data); | ||
| 26 | } | ||
| 27 | else { | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | sub previous_record { | ||
| 33 | my $self = shift; | ||
| 34 | my $time = shift || time; | ||
| 35 | my $url = $self->_url . 'previous/' . $time; | ||
| 36 | my $response = $self->_agent->get($url); | ||
| 37 | |||
| 38 | if ($response->is_success) { | ||
| 39 | my $data = XMLin($response->decoded_content); | ||
| 40 | return NIST::Beacon::Record->new($data); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub current_record { | ||
| 48 | my $self = shift; | ||
| 49 | my $time = shift || time - 60; | ||
| 50 | my $url = $self->_url . $time; | ||
| 51 | my $response = $self->_agent->get($url); | ||
| 52 | |||
| 53 | if ($response->is_success) { | ||
| 54 | my $data = XMLin($response->decoded_content); | ||
| 55 | return NIST::Beacon::Record->new($data); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub next_record { | ||
| 63 | my $self = shift; | ||
| 64 | my $time = shift or return; | ||
| 65 | my $url = $self->_url . 'next/' . $time; | ||
| 66 | my $response = $self->_agent->get($url); | ||
| 67 | |||
| 68 | if ($response->is_success) { | ||
| 69 | my $data = XMLin($response->decoded_content); | ||
| 70 | return NIST::Beacon::Record->new($data); | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ | ||