Category: |
Utility Scripts |
Author/Contact Info |
Terrence Hamilton - ersatz@netspace.net.au |
Description: |
This script will generate a 9 character random password, consisting of 3 uppercase characters, 3 lowercase characters & 3 numbers. |
#!/usr/bin/perl
$uppercase = join '', $uppercase, A..Z;
$lowercase = join '', $lowercase, a..z;
$numbers = join '', $numbers, 0..9;
for ($i = 0; $i < 3; $i++) {
$tmp = join '', $tmp, substr $uppercase, int rand length $uppercase,
+ 1;
$tmp = join '', $tmp, substr $lowercase, int rand length $lowercase,
+ 1;
$tmp = join '', $tmp, substr $numbers, int rand length $numbers, 1;
};
while (length $tmp) {
$password = join '', $password, substr $tmp, int rand length $tmp, 1
+, '';
};
print "Password: $password\n";
|
Re: Random Password Generator
by radiantmatrix (Parson) on Jul 05, 2005 at 14:04 UTC
|
Hm. Some flaws, like forcing a particular pattern, not using printable chars outside of A-Za-z0-9, etc. Mostly, though, reinventing a well-solved wheel.
use Crypt::RandPasswd;
print 'Password: ',Crypt::RandPasswd->chars(9,9), "\n";
The above generates a 9-char random password using Crypt::RandPasswd. If limiting the chars used, etc, is desired, read the POD for that module, as it explains how to restrict character space and other features.
If you're on a UNIX system, importing Crypt::Random will give you access to a better PRNG (using /dev/random or the EGD) that you can hand off to Crypt::RandPasswd. If you're not on UNIX, you can either install a version of EGD for your platform, or try using Crypt::Random::ISAAC - secure random number generator (which, admittedly, is something I wrote for solving the cryptographic randomness issue in pure Perl). EGD (or /dev/random) with Crypt::Random is by far the better choice, if it is available to you (better tested, more mature, etc.)
Larry Wall is Yoda: there is no try{}
The Code that can be seen is not the true Code
| [reply] [d/l] |
Re: Random Password Generator
by ihb (Deacon) on Jul 03, 2005 at 00:35 UTC
|
use String::Random qw/ random_string /;
use List::Util qw/ shuffle /;
print random_string(join '', shuffle((qw/ c C n /) x 3));
ihb
See perltoc if you don't know which perldoc to read!
| [reply] [d/l] |
Re: Random Password Generator
by sh1tn (Priest) on Jul 02, 2005 at 14:08 UTC
|
print+('a'..'A','A'..'1','0'..'9')[int rand(64)]for 1..9
| [reply] [d/l] |
|
| [reply] [d/l] |
Re: Random Password Generator
by Anonymous Monk on Jul 01, 2005 at 07:51 UTC
|
That is very nice and simple unlike a lot of other ones i have seen here at the monastery. *THUMBS UP* | [reply] |
|
my @ranges = ([a..z]) x 3, ([A..Z]) x 3, ([0..9]) x 3;
my $password;
while (@ranges) {
$range = splice @ranges, rand @ranges, 1; # random pick
$password .= $range->[rand @$range]; # random pick from the range
}
The whole thing of $password = join "", $password, ... in the original post is definitely worth unlearning if nothing else. {grin}
| [reply] [d/l] [select] |
|
@chr = (0..9,'A'..'Z','a'..'z');
print @chr[rand @chr] for 1..9;
but you don't get anymore the 3-3-3 pattern, which IMHO, isn't a big deal anyway.
| [reply] [d/l] |
|
|
This was actually a little experiment. I know the more practical way would be to use arrays, like in your post.
I was just trying to find the best way to do it using only strings. :o)
| [reply] |
|
I'm the original anon poster.. (-- I'll make an account one of these days) and umm.. what variable in your improvement is the password at the end? I can't seem to work it out. I know, I know. I'm dumb.
| [reply] |
|
|