OS/2 Fixpacks On CD
[Previous]
The Rexx File - by Dr. Dirk Terrell
[Next]
OS/2 e-Zine!

Go to a Printer Friendly version of this page



Dr. Dirk Terrell is an astronomer at the University of Florida specializing in interacting binary stars. His hobbies include cave diving, martial arts, painting and writing OS/2 software such as HTML Wizard.

Related Articles
Open folder
Recursive delete
Disk usage
Sorting stems
Advanced math
Automatic archiving
Browsers and cookies


Blast Back! Send a private message directly to Dr. Dirk Terrell with your thoughts:

Contact OS/2 e-Zine!

Sponsored links:
The OS/2 Supersite
WarpCast

Summary: Dirk shows how to do simple data type verification and random selection with this password generator program.

Being a system administrator, I frequently set up accounts for users and that requires my coming up with good passwords. Even if you're not a system administrator, you probably still need to come up with passwords on occasion. I recently wrote a little Rexx program to generate random passwords of a given length.

We will want to be able to specify the number of characters in the password on the command line , like this:

passwd 8
to generate a password consisting of eight characters. As usual we use the Parse Arg statement to read in the command line parameters:
Parse Arg NumChars
We should check that the user entered a valid value and if not, ask for it:
Do While DataType(NumChars)<>"NUM"
   Say "Enter the number of characters for the password:"
   Parse Pull NumChars
End
The DataType() function, when passed a single value, will return "NUM" if the string is a valid number or "CHAR" if not. Since we need a numerical value, we place it in a loop that checks to make sure that the user enters a number.

Not all characters are valid for passwords, notably non-printed characters like tabs and linefeeds. To make our program more flexible, we will create a stem variable (called Bag) that contains the valid characters that can be used. From that we will randomly choose characters. Another stem variable (Range) that will contain the ranges of valid characters. For example, let's say that we want the passwords to contain the following characters:

  • the digits 0-9
  • lowercase letters, a-z
  • uppercase letters, A-Z

So, in that case we set the values of Range would be set like this:

Range.1="0-9"
Range.2="a-z"
Range.3="A-Z"
Range.0=3

Note that we set the zeroth element of the array to contain the number of entries so that we can use a loop later to get at all of the elements. Now that we have a list of the valid character ranges, we can set up our Bag variable that is a list of all those characters:

Do i=1 to Range.0
   Parse Var Range.i StartC "-" StopC
   Start=C2D(StartC)
   Stop=C2D(StopC)
   Do j=Start to Stop
      k=k+1
      Bag.k=d2c(j)
   end /* do */
end /* do */
Bag.0=k

The outer loop covers the ranges. Within a given range we parse the value to get the beginning and ending ASCII values for the range. This we accomplish with the Parse statement using "-" as the separator. Then we use the C2D() function (character to decimal) to get the ASCII values. Once we have those, we run a Do loop and add the characters to the Bag stem variable, incrementing a counter variable k to keep track of how many characters are in Bag. After the loop has completed, we copy the count variable into the zeroth element of Bag.

Finally we are ready to compute our password. All we need to do is randomly pick the desired number of characters from Bag:

Passwd=""
Do i=1 to NumChars
   k=Random(1,Bag.0)
   Passwd=Passwd||Bag.k
end /* do */
Say Passwd
Exit

First we initialize a variable Passwd that will hold our generated password. Then we use a Do loop that runs over the number of characters we need. Inside that loop we use the Random() function to generate a random number between one and the number of characters in Bag. We then take that element from Bag and append it to Passwd. Once through the loop, we print out the password.

To add additional ranges of characters, simply add additional elements to the Range variable, ensuring that you adjust the value in Range.0 to reflect the number of elements. Now maybe you will have an easier time coming up with good passwords.

Download a copy of this month's Rexx File code.

[Previous]
 [Index]
 [Feedback]
 [Next]
Copyright © 1999 - Falcon Networking ISSN 1203-5696
May 1, 1999