Simple Program to Make Passwords
This is a test program for creating passwords.
Several points need to be made.
- srand, which initializes the random number generator, should be turned off
while debugging, and then turned on for production.
When turned off, the same sequence of random numbers will be generated time after
time.
When turned on, a completely new sequence of random numbers will be generated each
time the program is run, which corresponds better to our idea of a random number.
- We have arbitrarily limited passwords to length 5 in the while statement.
- We have used the concatenate operator (period, `.') to append a random
character 5 times to the emerging password.
The output string $o starts as an empty string, then grows 5 times by appending
a single random character.
That random character comes from a substring of the string $t.
- We choose A..Za..z1..0 as the string, since we want printable (and typeable)
characters in the student's passwords.
#!/usr/local/bin/perl -- -*- C -*-
# this is 'make_password.pl'
#srand;#this initializes the random number generator
# at the outset, while debugging, comment this out
srand;#this initializes the random number generator
$t='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijzklmnopqrstuvwxyz1234567890';
$i = 0;
$o = '';
while ($i<5){
$i++;
$o = $o.substr($t,rand(62)+1,1);
}
print $o;
Now, we convert this program into a subroutine, so that we can use it
in multiple places inside a progam without retyping the same lines of code, and
we can use the same scheme in multiple programs (if needed).
What follows is a translation of the above into an internal subroutine,
which is called at the line showing `&make_rno'.
Note the return statement, which assigns the subroutine's value.
#!/usr/local/bin/perl -- -*- C -*-
# this is 'make_password_sub.pl'
print "password = ",&make_rno;
sub make_rno {
srand;#this initializes the random number generator
$t='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijzklmnopqrstuvwxyz1234567890';
$i = 0;
$o = '';
while ($i<5){
$i++;
$o = $o.substr($t,rand(62)+1,1);
}
return $o;
}
The only adjustment we make to this subroutine form is to pass a length
as an argument, 7 in the example below, so that we can create longer
(or shorter) passwords.
#!/usr/local/bin/perl -- -*- C -*-
# this is 'make_password_sub.pl'
print "password = ",&make_rno(7);
sub make_rno {
srand;#this initializes the random number generator
$t='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijzklmnopqrstuvwxyz1234567890';
$i = 0;
$o = '';
while ($i<$_[0]){
$i++;
$o = $o.substr($t,rand(62)+1,1);
}
return $o;
}
where
while ($i<$_[0]){
is the adjusted line in the subroutine, where we have taken the first
argument and used it as a limit for the while statement.
Here is a more sophisticated version:
#!/usr/local/bin/perl -- -*- C -*-
# this is 'make_pass_2.pl'
#
# credit to shelden@spoke.law.cornell.edu
#
#srand;#this initializes the random number generator
# at the outset, while debugging, comment this out
srand($$|time);#this initializes the random number generator
$ascii_passwd = &pw_generate(5);
print $ascii_passwd;
sub pw_generate{
local(@passset,$rnd_passwd,$randum_num);
local ($randum_num);
#since l = both `el' and one, do not use
#since 0 = both `oh' and zero, do not use
@passset = ('a'..'k','m'..'z','A'..'N','P'..'Z','2'..'9');
$rnd_passwd = "";
for ($i = 0; $i<$_[0];$i++){
$randum_num = int(rand($#passset+1));
$rnd_passwd .= @passset[$randum_num];
}
return $rnd_passwd;
}