Random String/Password Generation

This tutorial will guide you on creating a custom tag that will generate random strings or passwords. This comes in handy when you are wanting to create a random password for a new user signup, etc..

Let's begin...

Ok, we must specify some values by default... this can be passed via the custom tag call <cf_tagName foo="123"> and so forth... but it is always a good idea to define values by default.

<!--- default length of the password to be generated --->
<cfparam name="ATTRIBUTES.Length" default="6">

<!--- types of characters to use in the password (default Uppercase, Lowercase letters and 0-9 --->
<cfparam name="ATTRIBUTES.CharSet" default="alpanumeric">

<!--- name of the variable to store the generated password (default RandomPassword) --->
<cfparam name="ATTRIBUTES.ReturnVariable" default="RandomPassword">

<!--- yes/no include uppercase characters in the password? (default yes)--->
<cfparam name="ATTRIBUTES.Ucase" default="yes">


The next thing we will implement, is some error handling. This will make sure that the tag has all the needed values available at execution time.

<cfif NOT isnumeric(ATTRIBUTES.Length)>
    <cfthrow detail=
"The attribute <b>Length</b> must be a valid numeric value">
</cfif>


<!--- valid CharSets are handled as the defaultcase below --->

<!--- ATTRIBUTES.Ucase is only checked for as "YES" if it is NOT "YES" uppercase letters are simply not included
no errors will be thrown no matter what you specify as this attribute--->


Next, we will define a few variables with sifferent types of data, these are used later on when generating the random string/password.
<cfset alphaLcase  = "a|c|e|g|i|k|m|o|q|s|u|w|y|b|d|f|h|j|l|n|p|r|t|v|x|z">
<cfset alphaUcase =
"A|C|E|G|I|K|M|O|Q|S|U|W|Y|B|D|F|H|J|L|N|P|R|T|V|X|Z">
<cfset numeric      =
"0|2|4|6|8|9|7|5|3|1">

Then, the next step is to actually decide what type of password we are generating. If all lowercase, all uppercase, both upper and lowercase, numeric, etc... (You get the point ;)

<!--- decide what cars to use in generating the password --->
<cfswitch expression="#ATTRIBUTES.CharSet#">
    <!--- Create only alpha passwords (NO NUMBERS ALLOWED) --->
    <cfcase value=
"alpha">
        <cfset charlist = alphaLcase>
        <cfif ATTRIBUTES.UCase IS
"Yes">
            <cfset charList = listappend(charlist, alphaUcase,
"|")>
        </cfif>
    </cfcase>

    <!--- Create Alphanumeric passwords --->
    <cfcase value="alphanumeric">
        <cfset charlist =
"#alphaLcase#|#numeric#">
        <cfif ATTRIBUTES.UCase IS
"Yes">
            <cfset charList = listappend(charlist, alphaUcase,
"|")>
        </cfif>
    </cfcase>

    <!--- Create only numeric passwords --->
    <cfcase value="numeric">
        <cfset charlist = numeric>
    </cfcase>

    <cfdefaultcase>
        <cfthrow detail=
"Valid values of the attribute <b>CharSet</b> are Alpha, AlphaNumeric, and Numeric">
    </cfdefaultcase>
</cfswitch>


<cfparam name="ThisPass" default="">

<!--- each loop count gets one new character and adds it to the end of the password,
so loop from 1 to "the number set in the calling template in ATTRIBUTES.Length --->

<cfloop from="1" to="#ATTRIBUTES.Length#" index="i">

    <!--- pick a random number between 1 and the length of the list of chars to use --->
    <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>

    <!--- get the character that is at the random numbers position in the list of characters --->
    <cfset ThisChar = ListGetAt(Charlist, ThisNum, "|")>

    <!--- append the new random character to the password --->
    <cfset ThisPass = ListAppend(ThisPass, ThisChar, " ")>

</cfloop>


<cfset ThisPass = replace(ThisPass, " ", "", "ALL")>
<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = ThisPass>

That's it... you have created the entire custom tag. Now save this file with the following name "RANDOM_PASS.cfm" and save it into your CustomTags folder (By Default: C:\CFusionMX\CustomTags\)

The last thing I will show you is how you WILL call the custom tag, this is the usage syntax for this upcoming tutorial...
 

<CF_RANDOM_PASS LENGTH="10"
                           
<!--- Number of cahracters to use in the password. Default is 6 --->
                            CHARSET="AlphaNumeric"

                                   <!--- Types of characters to use in the password Alph/Numeric/Alphanumeric.
                                   Default is Alphanumeric (letters and numbers) --->

                            UCASE="No"
                           
<!--- Use Uppercase letters in the password?? default is YES --->
                            RETURNVARIABLE="pword"
                           
<!--- Name of the variable to store the password in (default is RandomPassword)--->
                                  
>

<cfoutput>
#pword#</cfoutput>

All ColdFusion Tutorials By Author: Bobby Hartsfield
  • Another Random Image Selector
    I have seen a couple random image scripts here and there. Most require you to have a list of the images stored in a database somewhere or actually a switch statement with all the images listed there... This code will read a directory, pull all jpg's and choose a random one everytime to be displayed. Alot of my clients have wanted random images. THis is the easiest way I came up with to do it. This way when they want another image in the rotation... all you have to do is upload it to the image directory and its ready to go.
    Author: Bobby Hartsfield
    Views: 19,236
    Posted Date: Tuesday, August 12, 2003
  • Random String/Password Generation
    This is a custom tag I wrote that generates a random string at the set number of characters using the characters you decide on Alpha AlphaNumeric or Numeric Uppercase, lowercase, mixed case... It will create a variable that you name as you wish and call however you'd like. It is a small example of how to create a custom tag and will hopefully send someone in the right direction. It is also fully functional and has come in handy quite a few times. Enjoy and as always.. Have Fun!
    Author: Bobby Hartsfield
    Views: 25,762
    Posted Date: Tuesday, August 12, 2003
  • A Basic Calendar Application to Build on
    I have seen quite a few people looking for prebuilt calendar apps but the people I wrote this for are the people who wanted a headstart in building their OWN custom calendar app. It is very basic and does nothing more than display the currently selected month/year's calendar and give links to the PREVIOUS the NEXT month's calendars by using The DateAdd() function. Basically the backbone of any calendar app. Enjoy. and as Always.. HAVE FUN!
    Author: Bobby Hartsfield
    Views: 38,433
    Posted Date: Friday, August 15, 2003
Download the EasyCFM.COM Browser Toolbar!