Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Random Password Generator

by satz (Initiate)
on Jul 01, 2005 at 07:43 UTC ( [id://471577]=sourcecode: print w/replies, xml ) Need Help??
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";
Replies are listed 'Best First'.
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
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!

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


      Pugs Perl6 version: print ('a'..'z','A'..'Z',0..9).pick for 0..8;


      ___________
      Eric Hodges
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*
      It is simpler than many, but still unecessarily complicated, because of the amount of repeated code.

      Presuming you want 3 each of the lowercase, uppercase, and digits, you can simplify the code to:

      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}

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Or even simpler:

        @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.

        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)
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://471577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2025-06-22 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.