Hacking Linux Exposed

About
Authors
Contents
Reviews
Foreword
Purchase

Articles
Books
Sourcecode
Tools
Errata

Home

 


(view this code in a separate window)

#!/usr/bin/perl -w
# passwd_generator.pl
#
# Handy random password generator.
#
# Copyright 2001, James Lee.
# Released under GPL.

use strict;
my @chars = (33..91,93..126);
my $num_chars = @chars;
my $length;
my $funny = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';

print "Enter number of characters in your password: ";
chomp($length = <STDIN>);
die "Length must be greater than 6!" if $length <= 5;

while (1) {
    my $password = '';
    foreach (1..$length) {
        $password .= chr($chars[int(rand($num_chars))]);
    }
    if ($password =~ /[a-z]/ and $password =~ /[A-Z]/ and
        $password =~ /[0-9]/ and $password =~ /[$funny]/) {
        print $password, "\n";
        exit;
    }
}