Crypt::Random::Source::Weak::openssl - Get random bytes from the OpenSSL


Crypt-Random-Source-Weak-openssl documentation Contained in the Crypt-Random-Source-Weak-openssl distribution.

Index


Code Index:

NAME

Top

Crypt::Random::Source::Weak::openssl - Get random bytes from the OpenSSL command line utility

SYNOPSIS

Top

	use Crypt::Random::Source::Strong::openssl;

	my $source = Crypt::Random::Source::Weak::openssl->new

	my $bytes = $source->get(1024); # get 1kb of random bytes

DESCRIPTION

Top

This is a weak random byte source because openssl rand is a PRNG.

This is a subclass of Crypt::Random::Source::Base::Proc.

Due to the retarded nature of the rand command line utility's interface, it must repeatedly be invoked with default_chunk_size as number of random bytes to generate.

ATTRIBUTES

Top

default_chunk_size

The default number of bytes to generate per openssl rand invocation.

Defaults to 64 kb, which is pretty large and balances well with the startup time of openssl rand for miniscule chunks.

If you will be needing a lot of random data, increasing this number to something much larger would probably be beneficial.

openssl

The openssl executable to invoke. Defaults to what File::Which found for openssl (which means it must be in your PATH).

SEE ALSO

Top

Crypt::Random::Source

openssl(1), rand(1)

VERSION CONTROL

Top

This module is maintained using Darcs. You can get the latest version from http://nothingmuch.woobling.org/code, and use darcs send to commit changes.

AUTHOR

Top

Yuval Kogman <nothingmuch@woobling.org>

COPYRIGHT

Top


Crypt-Random-Source-Weak-openssl documentation Contained in the Crypt-Random-Source-Weak-openssl distribution.

#!/usr/bin/perl

package Crypt::Random::Source::Weak::openssl;
use Moose;

use File::Which qw(which);

use namespace::clean -except => [qw(meta)];

our $VERSION = "0.02";

sub available {
	which("openssl");
}

extends qw(
	Crypt::Random::Source::Weak
	Crypt::Random::Source::Base::Proc
);

has openssl => (
	is => "rw",
	default => sub { which("openssl") },
	trigger => sub { shift->clear_command },
);

has default_chunk_size => (
	is => "rw",
	default => 1 << 16,
	trigger => sub { shift->clear_command },
);

has 'command' => (
	isa => "ArrayRef",
	is  => "ro",
	lazy_build => 1,
	clearer => "clear_command",
);

sub _build_command {
	my $self = shift;
	return [$self->openssl, "rand", $self->default_chunk_size];
}

sub BUILD {
	my $self = shift;
	$self->set_default_command;
}

sub set_default_command {
	my $self = shift;

	$self->default_chunk_size(1 << 16) # about 1.5x the overhead of simply spawning openssl rand 0 on my computer
		unless $self->default_chunk_size;

	$self->command([qw(openssl rand), $self->default_chunk_size])
		unless defined $self->command;
}

sub _read_too_short {
	my ( $self, $buf, $got, $req ) = @_;

	$self->close; # will cause openssl to be respawned

	return $buf . $self->get( $req - $got );
}

__PACKAGE__

__END__