Welcome to

The Conxsys Blog

Thoughts on Conxsys, Notes/Domino, LotusScript, & Technology

LotusScript Random Password Generator

February 9th, 2012 by Corey Davis

I am working on a small project for a customer which, when complete, will be uploaded to OpenNTF. Part of the project required a random password generator so I set about searching for one. And I did find some, but none that did what I needed. I needed variable length passwords and the option to use special characters for added security. I could have just used one of the examples I found, but since they did not have a license associated with them and I want to upload the finished product to OpenNTF, I opted to write my own. As usual, I will walk you through the code.

1: Public Function GeneratePassword(length As Integer, useSpecialChar As Boolean) As String
2:	On Error GoTo ErrHandler

The function is called GeneratePassword and it accepts two parameters: the length of the password as an Integer, and whether or not to use special characters as a Boolean. And, of course, there is error handling.

4:	Randomize
6:	Dim pass As String
7:	Dim charCount, char As Integer
9:	If length > 63 Then Let length = 63	'Notes passwords cannot be more than 63 characters long

We start by calling the Randomize statement in order to seed LotusScript’s random number generator Rnd(), and declaring the variables we are going to use. We then make sure that the password length is not more than 63 characters since that is Domino’s password length limit.

11:	For charCount = 1 To length
12:		Let char = Round((Rnd() * 1000), 0)
13:		If useSpecialChar And (CInt(char) >= 33 And CInt(char) =< 126) Then
14:			Let pass = pass & Chr$(char)
15:		ElseIf (CStr(char) Like "[4][8-9]") Or (CStr(char) Like "[5][0-7]") Or _
16:		(CStr(char) Like "[6][5-9]") Or (CStr(char) Like "[7-8][0-9]") Or _
17:		(CStr(char) = "90") Or (CStr(char) Like "[9][7-9]") Or _
18:		(CStr(char) Like "[1][0-1][0-9]") Or (CStr(char) Like "[1][2][0-2]") Then
19:			Let pass = pass & Chr$(char)
20:		Else
21:			Let charCount = charCount - 1
22:		End If
23:	Next charCount

Now we move on to the heart of the function. We are going to loop through the code until we have generated a password that is the requested length. The first thing we will do, in line 12, is generate a random number (remember, Rnd() generates a random number between 0 and 1 such as .4285735) and then convert it and round it to a whole number. On line 13 we will check to see if we are allowed to use special characters and, if so, we will then see if our random number falls between 33 and 126. If so, we will get the character represented by that number and add it to our new password in line 14. If we are not using special characters, we will then move on to lines 15-18 and check to see if our number is 48-49 or 50-57 or 65-69 or 70-89 or 90 or 97-99 or 100-119 or 120-122. Yikes! If you step back from that a little, we are actually just checking to see if the number is between 48-57 (which, when run through Chr$() are the numbers 0-9), or 65-90 (a-z), or 97-122 (A-Z). There are a number of ways in which to handle this part of the task, and both of the other random password generators that I linked to above handle it differently, so you may want to look at those. So, just like before, if our number matches we will get the character represented by the number and add it to our new password. In lines 20-21, when none of the above match, we will decrement our counter since we did not add a character to the password. Finally we will run through the loop again until we have the correct password length.

Note: as I sit here writing this, I wonder if I should have used a Do/While loop rather than For/Next since I am modifying the counter within the loop. It works just fine, but may not be best practice.
25:	Let GeneratePassword = pass
27:	Exit Function
30:	Error Err, Error & { on line } & Erl & { in } & GetThreadInfo(1) & Chr$(13)
31:End Function

Finally, we will assign the new password to GeneratePassword in order to pass it back to the calling code and exit the function. We also have our error handling. We won’t really handle the error so much as trap it and bubble the error up to the calling code to handle.

In the end, we get random passwords like “1L4fXmEX” or, with special characters turned on, like “e$+E6f}d”.

You can download this function below, or it will be available as part of an OpenNTF project to be announced in the next couple of weeks.


Posted in Domino, SnTT, 14,254 views, 2 Comments
Digg This Submit to del.icio.us Submit to Technorati  

2 Responses

  1. Don Says:

    Nice work! I’m going to use this!

  2. Enrico Scotti Says:


Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

CC-GNU GPL Creative Commons License
The Conxsys Blog by Corey Davis is licensed under a Creative Commons Attribution 3.0 Unported License unless otherwise specified. Based on a work at conxsys.com/blog. All code on this blog is licensed under the CC-GNU GPL version 2.0 or later unless otherwise specified.

Copyright © 2006-2010 by Conxsys | Login | Powered by Wordpress | Template based on a design by Design4