Crypt-GeneratePassword

 view release on metacpan or  search on metacpan

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

my @signs = ('0'..'9', '%', '$', '_', '-', '+', '*', '&', '/', '=', '!', '#');
my $signs = join('',@signs);
my @caps = ('A' .. 'Z');
my $caps = join('',@caps);

my @set = (
	   [ ["\x00",'a'..'z'], ["\x00",'a'..'z',@caps] ],
	   [ ["\x00",'a'..'z',@signs], ["\x00",'a'..'z',@caps,@signs] ]
	  );

sub chars($$;$@) {
  my ($minlen, $maxlen, $set, @restrict) = @_;
  $set ||= $set[1][1];
  my $res;
  my $diff = $maxlen-$minlen;
  WORD: {
    $res = join '', map { $$set[random_number(scalar(@$set))] } 1..$maxlen;
    $res =~ s/\x00{0,$diff}$//;
    redo if $res =~ m/\x00/;
    for (my $i = 0; $i < @restrict; $i+=2) {
      my $match = $restrict[$i];

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

quad/trigrams must have for a word to be selected. Both are values between 0.0
and 1.0, specifying the percentage of the maximum frequency. Higher
values create less secure, better pronounceable passwords and are slower.
Useful $minfreq values are usually between 0.001 and 0.0001, useful $avgfreq
values are around 0.05 for trigrams (word3) and 0.001 for quadgrams (word).

=cut

our $total;

sub word($$;$$$$$)
{
    my $language = splice(@_,2,1) || '';
    $language =~ s/[^a-zA-Z_]//g;
    $language ||= $default_language;
    eval "require Crypt::GeneratePassword::$language";
    my $lang = $languages{$language};
    die "language '${language}' not found" if !$lang;

    my ($minlen, $maxlen, $numbers, $capitals, $minfreq, $avgfreq) = map { int($_) } @_;
    $minfreq ||= 0;

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

            $k0 = $k1;
            $k1 = $k2;
            $k2 = $k3;
        }
        redo if $sum/length($stripped) < $avgfreq;
        redo if (restrict($stripped,$language));
        return $randword;
    }
}

sub word3($$;$$$$$)
{
    my $language = splice(@_,2,1) || '';
    $language =~ s/[^a-zA-Z_]//g;
    $language ||= $default_language;
    eval "require Crypt::GeneratePassword::$language";
    my $lang = $languages{$language};
    die "language '${language}' not found" if !$lang;

    my ($minlen, $maxlen, $numbers, $capitals, $minfreq, $avgfreq) = map { int($_) } @_;
    $minfreq ||= 0.01;

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

theoretical password space was actually considered a
pronounceable password. Since this analysis is only
statistical, it proves absolutely nothing if you are deeply
concerned about security - but in that case you should use
chars(), not word() anyways. In reality, it says a lot
about your chosen parameters if you use large values for
$count.

=cut

sub analyze($@) {
    my $count = shift;
    $total = 0;
    for (1..$count) {
        my $word = &word(@_);
    }
    return $count/$total;
}

sub analyze3($@) {
    my $count = shift;
    $total = 0;
    for (1..$count) {
        my $word = &word3(@_);
    }
    return $count/$total;
}

=head2 generate_language

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

element or a file handle or name to read words from, one word per line7.
Alternatively, you may pass an array directly, not as reference.
A language description is about 1MB in size.

If you generate a general-purpose language description for a
language not yet built-in, feel free to contribute it for inclusion
into this package.

=cut

sub generate_language($@) {
  my ($wordlist) = @_;
  if (@_ > 1) {
    $wordlist = \@_;
  } elsif (!ref($wordlist)) {
    $wordlist = [ split(/\s+/,$wordlist) ];
    if (@$wordlist == 1) {
      local *FH;
      open(FH,'<'.$$wordlist[0]);
      $wordlist = [ <FH> ];
      close(FH);

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

language as the fifth parameter of words(). You should use the
well-known ISO two letter language codes if possible, for best
interoperability.

If you specify $default with a true value, this language will
be made global default language. If you give undef as
$language_description, only the default language will be changed.

=cut

sub load_language($$;$) {
  my ($desc,$name,$default) = @_;
  $languages{$name} = eval $desc if $desc;
  $default_language = $name if $default;
}

=head2 random_number

  $number = random_number($limit);

Returns a random integer between 0 (inclusive) and C<$limit> (exclusive).

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

      # suppress warning about function being redefined
      no warnings 'redefine';
      *Crypt::GeneratePassword::random_number = \&my_rng;
    }

The default implementation uses perl's rand(),
which might not be appropriate for some sites.

=cut

sub random_number($) {
  return int(rand()*$_[0]);
}

=head2 restrict

  $forbidden = restrict($word,$language);

Filters undesirable words. Returns false if the $word is allowed
in language $lang, false otherwise. Change this to a function of
your choice by doing something like this:

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

      *Crypt::GeneratePassword::restrict = \&my_filter;
    }

The default implementation scans for a few letter sequences that
english or german people might find offending, mostly because of
their sexual nature. You might want to hook up a regular password
checker here, or a wordlist comparison.

=cut

sub restrict($$) {
  return ($_[0] =~ m/f.ck|ass|rsch|tit|cum|ack|asm|orn|eil|otz|oes/i);
}

=head1 SEE ALSO

L<Crypt::RandPasswd>

=head1 REPOSITORY

L<https://github.com/neilb/Crypt-GeneratePassword>



( run in 0.333 second using v1.01-cache-2.11-cpan-1f129e94a17 )