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
#
# passwd_generator.pl
#
# random password generator
# (for different values of 'random'.)
#
# Creates a password of specified length that
# uses characters from several character classes.
#
# Copyright 2001, James Lee and Bri Hatch
#
# Released under the GPL.  See COPYING file
# for more information.

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


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 =~ /[$punct]/) {
		print $password, "\n";
		exit;
	}
}