Bytes-Random-Secure-Tiny

 view release on metacpan or  search on metacpan

lib/Bytes/Random/Secure/Tiny.pm  view on Meta::CPAN

use constant _backend => 0;

my %CSPRNG = (
    XS  => 'Math::Random::ISAAC::XS',
    PP  => 'Math::Random::ISAAC::PP',
    EM  => 'Math::Random::ISAAC::PP::Embedded',
);

sub new {
    my ($class, @seed) = @_;
    our $EMBEDDED_CSPRNG =
        defined $EMBEDDED_CSPRNG             ? $EMBEDDED_CSPRNG             :
        defined $ENV{'BRST_EMBEDDED_CSPRNG'} ? $ENV{'BRST_EMBEDDED_CSPRNG'} : 0;
    my $DRIVER =
        $EMBEDDED_CSPRNG                          ? $CSPRNG{'EM'} :
        eval {require Math::Random::ISAAC::XS; 1} ? $CSPRNG{'XS'} :
        eval {require Math::Random::ISAAC::PP; 1} ? $CSPRNG{'PP'} :
                                                    $CSPRNG{'EM'};
    return bless [$DRIVER->new(@seed)], $class;
}

sub irand {shift->[_backend]->irand}

1;

t/20-functions.t  view on Meta::CPAN

## no critic (RCS,VERSION,encapsulation,Module)

use strict;
use warnings;

use Test::More;
use Bytes::Random::Secure::Tiny;

use 5.006000;

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;
my @methods = qw/ bytes bytes_hex string_from /;

can_ok( 'Bytes::Random::Secure::Tiny', @methods );

my $r = Bytes::Random::Secure::Tiny->new(bits => 64);

foreach my $want ( qw/ 0 1 2 3 4 5 6 7 8 16 17 1024 10000 / ) {
  my $correct = $want >= 0 ? $want : 0;
  is( length $r->bytes( $want ), $correct,
      "bytes($want) method returns $correct bytes." );

t/21-bytes_random_tests.t  view on Meta::CPAN

## no critic (RCS,VERSION,encapsulation,Module)

use strict;
use warnings;

use Test::More;

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

eval "use Statistics::Basic;"; ## no critic (eval)
if ( $@ ) {
  plan( skip_all => "Statistics::Basic needed for random quality tests." );
}

use Bytes::Random::Secure::Tiny;

# Default seed entropy of 8 longs is more than we need in testing.
my $random = Bytes::Random::Secure::Tiny->new(bits=>64);

t/22-random_string_from.t  view on Meta::CPAN

## no critic (RCS,VERSION,encapsulation,Module)

use strict;
use warnings;
use Test::More;

use Bytes::Random::Secure::Tiny;

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

# We'll use a weaker source because we're testing for function, quality
# isn't being contested here.
my $random = Bytes::Random::Secure::Tiny->new(bits=>64);

for my $count ( 0 .. 11 ) {
  is( scalar @{$random->_ranged_randoms(16,$count)}, $count,
      "Requested $count ranged randoms, and got $count." );
}

t/23-string_from_unicode.t  view on Meta::CPAN

## no critic (RCS,VERSION,encapsulation,Module)

use strict;
use warnings;
use Test::More;
use Bytes::Random::Secure::Tiny;

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;
$ENV{'BRST_DEBUG'} = 1;

if( ! $^V ||  $^V lt v5.8.9 ) {
  plan skip_all =>
    'Cannot reliably test Unicode support on Perl\'s older than 5.8.9.';
}

binmode STDOUT, ':encoding(UTF-8)';

my $num_octets = 80;

t/24-oo_construct.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use 5.006000;
use Bytes::Random::Secure::Tiny;

# Test the constructor, and its helper functions.

can_ok( 'Bytes::Random::Secure::Tiny', qw/ new / );

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

# Instantiate with a dummy callback so we don't drain entropy.
my $random = new_ok 'Bytes::Random::Secure::Tiny' => [Bits=>128,NonBlocking=>1];

isa_ok $random, 'Bytes::Random::Secure::Tiny';
is $random->{'bits'}, 128, 'Seed is 128 bits.';

new_ok 'Bytes::Random::Secure::Tiny' => [NonBlocking=>0,Bits=>64]
    if $ENV{RELEASE_TESTING};

t/26-oo_public.t  view on Meta::CPAN

## no critic (RCS,VERSION,encapsulation,Module)

use strict;
use warnings;
use Data::Dumper;
use Test::More;
use Bytes::Random::Secure::Tiny;

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

# Public methods tested here (bytes(), etc.).
# Much of this has already been put through the paces via the "functions" layer
# tests in 20-functions.t, so we're only going for coverage here.

my $random = Bytes::Random::Secure::Tiny->new(bits => 64);

is( length $random->bytes(10), 10, 'bytes(10) returns ten bytes.' );

like( $random->bytes_hex(16), qr/^[1234567890abcdef]{32}$/,

t/27-fips140-1.t  view on Meta::CPAN

# abort installation, which is the "Perlish" solution.

use 5.006000;

use strict;
use warnings;
use Test::More;
use Bytes::Random::Secure::Tiny qw();
use Time::HiRes qw/gettimeofday/;

$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;
$ENV{'BRST_DEBUG'} = 1;

my $random = Bytes::Random::Secure::Tiny->new(bits=>128);

plan tests => 2 + 2 + 2 + 24 + 2;

my @rbytes;
push @rbytes, $random->bytes(1) for 1..2500;

# FIPS-140 test

t/28-sequence.t  view on Meta::CPAN

#!/usr/bin/perl -T

# Check the generated sequence against the reference implementation
use strict;
use warnings;
use Test::More;
use Bytes::Random::Secure::Tiny;
$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

my @results = (
   405143795,  806046349,  807101986, 2961886497,  695195257,
  2572289769, 3019876533,  264870948, 1594302383, 1378164207,
  2303672770, 3427572475, 2529378164,  880588573, 3373240253,
  2790847866, 3317866008, 1649015337, 3135336442,  633893309,
  4106706468, 1521740473, 2890206095,  371838884,    1501295,
  4068232461, 1734642455,   96489422, 1154744185, 3333214735,
  1194318228, 2216767940, 3939805440, 3223247373, 2657111163,
  4220128921,   48331631, 3904173607, 3338935982, 1121033431,

t/29-memory.t  view on Meta::CPAN

#!/usr/bin/perl -T

# Test for memory leaks

use strict;
use warnings;
use Test::More;
use Bytes::Random::Secure::Tiny;
$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

plan skip_all => 'This test is not compatible with Devel::Cover'
    if exists $INC{'Devel/Cover.pm'};

eval {require Test::LeakTrace};

plan skip_all => 'Test::LeakTrace required to test memory leaks' if $@;
plan tests    => 1;

Test::LeakTrace->import;

t/30-uniform.t  view on Meta::CPAN

#!/usr/bin/env perl

# Check the generated sequence for uniformity

use strict;
use warnings;
use Test::More;
use Bytes::Random::Secure::Tiny;
$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG=1;

eval 'require Statistics::Test::RandomWalk; 1;' ## no critic (eval)
    or plan skip_all => 
        'Statistics::Test::RandomWalk not available for testing.';

my $no_bins = 20;
plan tests => 3 * $no_bins;

# Default unseeded sequence
sub runtest {

t/31-crse_load.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Bytes::Random::Secure::Tiny;
$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;
use Test::More;

my $s = new_ok 'Crypt::Random::Seed::Embedded';
can_ok 'Crypt::Random::Seed::Embedded', 'new';
can_ok $s, qw(random_values);

done_testing();

t/32-crse_random_values.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Bytes::Random::Secure::Tiny;
$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

my $src = Crypt::Random::Seed::Embedded->new(NonBlocking=>1);

{
  my @vals = $src->random_values();
  is scalar @vals, 0, "random_values() returns empty array";
}
{
  my @vals = $src->random_values(undef);
  is scalar @vals, 0, "random_values(undef) returns empty array";

t/33-crse_coverage.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;

use Bytes::Random::Secure::Tiny;
$Math::Random::ISAAC::Embedded::EMBEDDED_CSPRNG = 1;

ok !defined Crypt::Random::Seed::Embedded::__read_file('/dev/urandom/',0),
    'CRSE::__read_file returns undef for requests of zero bytes.';

SKIP: {
    skip 'Blocking tests only happen in RELEASE_TESTING mode.', 2
        unless $ENV{RELEASE_TESTING};
    my $s = new_ok 'Crypt::Random::Seed::Embedded', [nonblocking => 0];
    ok eval {$s->random_values(10); 1}, 'Blocking source produces seed values.';
    my $r = new_ok 'Bytes::Random::Secure::Tiny', [nonblocking => 0, bits => 64];

t/35-mrie-cover.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;

use constant _backend => 0;
{
    local $ENV{BRST_EMBEDDED_CSPRNG} = 1;
    require Bytes::Random::Secure::Tiny;
    isa_ok new_ok('Bytes::Random::Secure::Tiny', [bits=>64])->{_rng}[_backend], 'Math::Random::ISAAC::PP::Embedded'; 
}

done_testing();



( run in 1.422 second using v1.01-cache-2.11-cpan-71847e10f99 )