#!/usr/bin/perl
# Author: Alex Efros <powerman-asdf@yandex.ru>, 2008
# License: Public Domain
# 
# genpw: Strong password generator.
# 
# Generate strong password, 10 characters long, with user-defined amount
# of alphanumeric characters (10 by default). Examples:
#   $ genpw
#   oKkW6Djp2M
#   $ genpw
#   b6IajHrMiB
#   $ genpw 6
#   I/Kkyq-!4+
#   $ genpw 4
#   ](SC]n=9>[

our $VERSION = 2.00;

my $SIZE     = 10;
my @ALPHANUM = ( 'A'..'Z', 'a'..'z', 0..9 );
my @COMPLEX  = map {chr} 33 .. 126;

sub rand_ch { # from "Perl Cookbook"
    my ($length, @chars) = @_;
    return join("", @chars[ map { rand @chars } 1 .. $length ]);
}

my $alphanum = @ARGV ? shift : $SIZE;
die "Usage: $0 [alphanumeric]\n\twhere 0<alphanumeric<=$SIZE\n"
    if !(0<$alphanum && $alphanum<=$SIZE);
my @chars = $alphanum == $SIZE ? @ALPHANUM : @COMPLEX;

do {
    $_ = rand_ch($SIZE, @chars);
} until abs(tr/A-Z//-tr/a-z//)<=1 && $alphanum==tr/A-Za-z0-9//;
print "$_\n";

