Generating random passwords with Perl

While performing some house cleaning this evening, I came across the following Perl nugget:


my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
my $randpassword = join '', map $alphanumeric[rand @alphanumeric], 0..8;
print "$randpassword\n"

This awesome little 3-line script will produce random 8-character alphanumeric passwords:



I wish I knew where I grabbed this from so I could publicly thank the author.

1 thought on “Generating random passwords with Perl”

  1. Neat. You could also expand $alphanumeric to include special characters so the generated passwords would pass the complexity checking used at some sites!@#!

    You can also generate random passwords with everybody’s favorite crypto library, OpenSSL:

    openssl rand 8 -base64

    And while we’re on the subject of passwords and crypto, can anybody tell me what purpose password expiration serves in the crypto era? It’s not 1980 anymore, and we’re not sending passwords across the network in cleartext anymore, so why force me to change my password every month? Seems to me all password expiration accomplishes is forcing users to write down their passwords because they change too often to remember.

Leave a Reply

Your email address will not be published. Required fields are marked *