Creating Random Passwords
Get the Code for this Tutorial
View a Working Example of This Code
Comment on this Tutorial
Creating a Random Password in Coldfusion
I needed to create new passwords for a project a while back for several reasons. First being that there are just some cases where it might be easier to create a password rather than to have the user type one in. An e-commerce site, for instance. You already have the users email address from the requires fields for ordering, why not create a random password and set up a user account for this user without them needing to think about it?
Second, a password reset function is much easier if you simply set the users password to something new and random. Of course this is only after they have received the reset password email and clicked the link to do so.
Anyway, it is simple, and there are a million ways to do it. I like to use a UDF (or CFC method if you want).
First set up the function with the argument 'passLen'. This is the optional password length.
<cffunction name="createPassword">
<cfargument name="passLen" default="6" required="No" type="numeric">
Next set a list of allowed characters. My list is usually like this, lowercase alphabet, 0-9, uppercase alphabet, 0-9. I do 0-9 twice so that there are a proportional amount of numbers available to characters. You can make this anything you want! I use hash() on almost every stored password, so I like the extra random-ness of both upper and lower case letters. Don't forget to var your initial variables here also. The variable newString is will be the final output, and the variable thisChar is used in the loop to create the password.
<cfset var charList = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789">
<cfset var newString = "">
<cfset var thisChar = "">
Using cfscript we can to a 'while' loop - a condition loop that can not be done in cfloop. I do not use cfscript usually so only this section is in script. You could make the entire UDF in cfscript.
We are looping until the password string (newString) is as long as our passlen argument. The default is 6.
<cfscript>
while(len(newString) LT arguments.passLen){
Continuing on the loop, get a random number between 1 and the length of your character list, minus one so that we do not try to get the next character after the end of the list, and error. Now we have a random position in the charList. Use that together with the mid function to get one character, and append it to the newString variable. This loop will run until the password is the correct length as specified in the arguments.
thisChar = randRange(1,len(charList)-1);
newString = newString & mid(charList,thisChar,1);
}
</cfscript>
Return the newString as your complete password, and close the cffunction tags.
<cfreturn newString>
</cffunction>
Usage
Create a random password by using the function. This will create a password 5 characters long.
<cfset myNewPassword = createPassword(5)>
____________
Tutorial Code
<!--------------------------------------------
Kevin Sargent
December 21th 2006
http://www.lot-o-nothin.com/cfml/
Please Give Credit Where Used.
--------------------------------------------->
<!--- create the function or method --->
<cffunction name="createPassword" output="No" returntype="string">
<!--- optional argument for password length --->
<cfargument name="passLen" default="6" required="No" type="numeric">
<!--- list of characters to choose from --->
<cfset var charList = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789">
<!--- var our varibles... --->
<cfset var newString = "">
<cfset var thisChar = "">
<!--- cfscript to perform a while loop --->
<cfscript>
// while our password length is less than our required password length specified in arguments..do...
while(len(newString) LT arguments.passLen){
// get a randome character from our charList and append it to the password string (newstring)
thisChar = randRange(1,len(charList)-1);
newString = newString & mid(charList,thisChar,1);
}
</cfscript>
<!--- return the newString, that is your password! --->
<cfreturn newString>
</cffunction>
Comments
silvester
07/04/2007
Plz show me how to use cffuction.
I am new to Cold fuction function
silvester
07/04/2007
Hi,
the working example has the cfform form script.
I would like to have that too, so that I can use the fuction code.
thank you