code:
#------------------------------------------------------------------------------------------------------------
# Initialize @word for password generation
#------------------------------------------------------------------------------------------------------------
my @words;
&InitGenWordPW (\@words);
sub InitGenWordPW (\@words)
{
# This subroutine populates an array of words from an external file.
# The array will be used by &GenWordPW to build an easier to remember PW
# This subroutine is called once per program execution to minimize IO.
my $WordsArrayRef = shift;
my $wordfile = $RootPath . "\\..\\pwdata\\wordlist.txt";
if (-e $wordfile)
{
&LogText($LogFile, "Password word list file found.");
if (open (WORDFILE, $wordfile))
{
local $/;
my $text = <WORDFILE>;
$text =~ s/\W+/\n/g;
@$WordsArrayRef = split /\n/,$text;
close INFILE;
}
else
{
&LogText($LogFile, "Error opening $wordfile: $^E");
}
}
else
{
&LogText($LogFile, "Password word list file NOT found: $wordfile");
}
}
sub GenWordPW (\@words)
{
# This subroutine generates a password that is easier to remember than GenRandomPW.
# The format of the password is word1, punctuation, word2, number Ex: cat@dog1
# The subroutine will first attempt to use an array that was populate externally by
# &InitGenWordPW. If the array is empty then the subroutine will populate the array using
# the words contained within the subroutine definition.
my $WordsArrayRef = shift;
my $password;
if (scalar @$WordsArrayRef == 0)
{
&LogText($LogFile, "PW word array not populated. Using default word list.");
@$WordsArrayRef = ('bird', 'cat' , 'dog', 'egg', 'pup', 'gas', 'pear', 'kiwi',
'bag', 'cap', 'oil', 'box', 'cup', 'jet', 'car', 'bus', 'zoo');
}
my @punc = ('!', '@', '#', '$', '%', '&', '*');
my @num = (2..9);
my $word1 = $$WordsArrayRef [rand (scalar @$WordsArrayRef)];
my $punc = @punc [rand (scalar @punc)];
my $word2 = $$WordsArrayRef [rand (scalar @$WordsArrayRef)];
my $num = @num [rand (scalar @num)];
if (($word1 && $punc && $word2 && $num) and (length($word1 . $punc . $word2 . $num) >= 8))
{
$password = "$word1$punc$word2$num";
}
else
{
&LogText($LogFile, "GenWordPW failed to provide an acceptable PW (" . $word1 . $punc . $word2 . $num . ") Generating random PW.");
$password = &GenRandomPW(6);
}
return $password;
}