First of all, while reading through the script in more detail, i noticed that not everything works as expected. For example, the "special characters" option was missing, no help/usage information on command line, version info on command line was missing too. So i've gone ahead and fixed that.
Frankly, i didn't really see the added value/security of using irand() from Math::Random::Secure in this specific use case. So i decided to change to rand() instead, which also reduces external dependencies and therefore generated file size.
Here's the updated source code:
#!/usr/bin/perl
#
# pwgen 1.0
#
# Usage: pwgen [-h] [-s] [length]
#
# length - an optional argument indicating the length of the
+password
# -s - use special characters
# -h - display usage
#
# This will generate random passwords of the specified or default leng
+th.
# Requires the Perl package Math::Random::Secure to produce
# cyptographically secure passwords.
#
# Copyright (C) 2012 - Paul E. Jones <paulej@packetizer.com>
# Permission to use, copy, modify and distribute this software is gran
+ted.
#
# Fixed, expanded and compiled by Rene "cavac" Schickbauer for PerlMon
+ks, see
# http://www.perlmonks.org/index.pl?node_id=946871
use strict;
use warnings;
use 5.010;
# Define the default password length
use constant DEFAULT_PASSWORD_LENGTH => 12;
#
# MAIN
#
# init to defaults
my $password_length = DEFAULT_PASSWORD_LENGTH;
my $use_special = 0;
# simple-minded grabbing of values from @ARGV
foreach my $arg (@ARGV) {
given($arg) {
when('-s') {
$use_special = 1;
next;
}
when('-h') {
showUsage();
exit(0);
}
when('-v') {
showVersion();
exit(0);
}
}
$arg = (0 + $arg); # convert to numeric
if($arg > 0) {
$password_length = $arg;
}
}
print GeneratePassword($password_length,$use_special) . "\n";
exit(0); # Finish without error
#
# GeneratePassword
#
# Description
# This routine will generate a password and return it as a str
+ing.
# By default, it will not utilize special characters like "~"
+in
# passwords, but if the second argument is a 1, it will. Note
+ that
# use of special characters provides only minimum additional s
+trenth,
# yet they are not very friendly for humans. For details, visi
+t
# https://secure.packetizer.com/pwgen/.
#
# Parameters
# length [in]
# The length of the password
# special [in]
# Indicates whether to use special characters other than
# the letters A-Z, a-z, and digits 0-9.
#
# Returns
# A string containing the password, or an empty string if ther
+e
# was an error producing the password.
#
sub GeneratePassword
{
my ($length, $special) = @_;
my $password = "";
my @pwchars = (
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c
+', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q
+', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E
+', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S
+', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '~', '`', '!', '@', '#', '$', '%
+', '^',
'&', '*', '(', ')', '_', '+', '=', '-', '{', '}', '|', '\\', '
+]', '[',
':', '"', '\'', ';', '<', '>', '?', '/', '.'
);
for(1 .. $length) {
if ($special) {
$password .= $pwchars[int(rand(93))];
} else {
$password .= $pwchars[int(rand(62))];
}
}
return $password;
}
sub showUsage {
print <<ENDUSAGE;
Usage: pwgen [-h] [-s] [length]
length - an optional argument indicating the length of the p
+assword
-s - use special characters
-h - display usage
-v - display version
ENDUSAGE
}
sub showVersion {
print <<ENDVERSION;
pwgen 1.0
This will generate random passwords of the specified or default lengt
+h.
Requires the Perl package Math::Random::Secure to produce
cyptographically secure passwords.
Copyright (C) 2012 - Paul E. Jones <paulej\@packetizer.com>
Permission to use, copy, modify and distribute this software is grant
+ed.
Fixed, expanded and compiled by Rene "cavac" Schickbauer for PerlMonk
+s, see
http://www.perlmonks.org/index.pl?node_id=946871
ENDVERSION
}
Ok, here is the link to the executable. Please tell me as soon as you downloaded it, i'll be deleting it from my server then.
"Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid… and I went ahead anyway." (Crow in "MST3K The Movie")
|