Crypt-Diceware

 view release on metacpan or  search on metacpan

lib/Crypt/Diceware.pm  view on Meta::CPAN

use 5.008001;
use strict;
use warnings;

package Crypt::Diceware;
# ABSTRACT: Random passphrase generator loosely based on the Diceware algorithm
our $VERSION = '0.005'; # VERSION

use Class::Load qw/load_class/;
use Crypt::Rijndael;
use Crypt::URandom;
use Data::Entropy qw/with_entropy_source/;
use Data::Entropy::Algorithms qw/pick_r/;
use Data::Entropy::RawSource::CryptCounter;
use Data::Entropy::Source;

use Sub::Exporter -setup => {
    exports => [ words   => \'_build_words' ],
    groups  => { default => [qw/words/] },
};

my $ENTROPY = Data::Entropy::Source->new(
    Data::Entropy::RawSource::CryptCounter->new(
        Crypt::Rijndael->new( Crypt::URandom::urandom(32) )
    ),
    "getc"
);

sub _build_words {
    my ( $class, $name, $arg ) = @_;
    $arg ||= {};
    my $list;
    my $entropy = $arg->{entropy} || $ENTROPY;
    if ( exists $arg->{file} ) {
        my @list = do { local (@ARGV) = $arg->{file}; <> };
        chomp(@list);
        $list = \@list;
    }
    else {
        my $word_class = $arg->{wordlist} || 'Common';
        unless ( $word_class =~ /::/ ) {
            $word_class = "Crypt::Diceware::Wordlist::$word_class";
        }
        load_class($word_class);
        $list = do {
            no strict 'refs';
            \@{"${word_class}::Words"};
        };
    }
    return sub {
        my ($n) = @_;
        return unless $n && $n > 0;
        my @w = with_entropy_source(
            $entropy,
            sub {
                map { pick_r($list) } 1 .. int($n);
            }
        );
        return wantarray ? @w : join( ' ', @w );
    };
}

1;


# vim: ts=2 sts=2 sw=2 et:

__END__

=pod

=encoding UTF-8

=head1 NAME

Crypt::Diceware - Random passphrase generator loosely based on the Diceware algorithm

=head1 VERSION

version 0.005

=head1 SYNOPSIS

  use Crypt::Diceware;
  my @phrase = words(4); # qw/starker call recur outlaw/

  # with alternate word lists
  use Crypt::Diceware words => { wordlist => 'Original' };
  use Crypt::Diceware words => { wordlist => 'Beale' };

=head1 DESCRIPTION

This module generates a random passphrase of words based loosely on the
L<Diceware|http://world.std.com/~reinhold/diceware.html> algorithm by Arnold G.
Reinhold.



( run in 2.768 seconds using v1.01-cache-2.11-cpan-a9496e3eb41 )