Generate Random Passwords in ColdFusion

This ColdFusion tutorial will generate random passwords based on a set of valid characters.


The generate passwords are particular business rules. These rules included:

1.    Must be exactly 8 characters in length
2.    Must have at least 1 number
3.    Must have at least 1 uppercase letter
4.    Must have at least 1 lower case letter


<!--- Set up available lower case values. --->
<cfset Str_Lower_Case_Alpha = "abcdefghijklmnopqrstuvwxyz" />

<!--- Set up available upper case values. This is done by converting the lower case string using "UCase". --->
<cfset Str_Upper_Case_Alpha = UCase( Str_Lower_Case_Alpha ) />

<!--- Set up available numbers. --->
<cfset Str_Numbers = "0123456789" />

<!--- Make a string that contains our lower case upper case and number values. --->
<cfset strAll = Str_Lower_Case_Alpha & Str_Upper_Case_Alpha & Str_Numbers />

<!--- Create the password array of 1-3 dimensions. Index array elements with square brackets: [ ]. --->
<cfset arrPassword = ArrayNew( 1 ) />

<!--- Generate the password and put each random value in the password array string. --->
<!--- Select the random letter from our lower case set. --->
<cfset arrPassword[ 1 ] = Mid(Str_Lower_Case_Alpha,RandRange( 1, Len( Str_Lower_Case_Alpha ) ), 1 ) />

<!--- Select the random letter from our upper case set. --->
<cfset arrPassword[ 2 ] = Mid(Str_Upper_Case_Alpha, RandRange( 1, Len( Str_Upper_Case_Alpha ) ), 1 ) />

<!--- Select the random number from our number set. --->
<cfset arrPassword[ 3 ] = Mid(Str_Numbers,RandRange( 1, Len( Str_Numbers ) ),1) />

<!--- Create rest of the password. --->
<cfset arrPassword[ 4 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) />
<cfset arrPassword[ 5 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) />
<cfset arrPassword[ 6 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) />
<cfset arrPassword[ 7 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) />
<cfset arrPassword[ 8 ] = Mid(strAll,RandRange( 1, Len( strAll ) ),1) />

<!--- We don?t always want the first three characters to be of the same order (by type). --->
<!--- Therefore, let's use the Java Collections utility class to shuffle this array into a "random" order. --->
<cfset CreateObject( "java", "java.util.Collections" ).Shuffle(arrPassword ) />

<!--- Join all the characters into a single string. --->
<!--- We can do this by converting the array to a list and then just providing no delimiters (empty string delimiter). --->
<cfset strPassword = ArrayToList(arrPassword, "" ) />

<!--- The password is finished. --->
<cfoutput>#strPassword#</cfoutput>

Some example password from this tutorial:

h29oCl4I
QaUsw7kY
2D8QqQop
LaynM6OG
761fmfuP

Advertisement

McAfee, Inc

User Comments: 1

 Wow, this is AWESOME

Hey, I've got a new site coming up on the fourth of July - www.WoundedInIraq.org and I was trying to figure out how to generate a random password automatically when a volunteer signed up.  This script looks awesome and I look forward to giving it a try... THANKS, VERY COOL!

ApplayIT is owned by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.