Categories

Category Specific RSS

Random password generator in PERL

Usage: perl script.pl length

 

#!/usr/bin/perl
# Script: Random password generator
# Fri 03 Dec 2010 01:30:24 PM CET
#use strict;

# List of chars. Modify this if you need to add more, like simbols, etc…
my @chars = qw( a b c d e f g h i j k l m n o p q r s t
u v w x y z A B C D E F G H I J K L M N
O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7
8 9 );

# The array of the password.
my @password = (“”);

# Select a random char from the array @chars and introduced into the array @password.
sub pass_gen {
my $rand = int(rand(@chars));
my $add_char = $chars[$rand];
push(@password, $add_char);
}

# The length of the password. If none is supplied externally, given the value of 8.
my $length = shift || 8;

# Call the function pass_gen X (the value stored in $lenght) times.
for (my $x = 1; $x <= $length; $x++) {
&pass_gen();
}

# Print the password without spaces.
print “Here is your password:\n”;
print @password = grep { “$_” } @password, “\n”;

#End of file

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Leave a Reply