Friday, September 19, 2008

Random password generator in CF

<cfset ststring=structNew()>
<cfloop index="i" from="1" to="6" step="1">

<!--- let ?a? be random number between 48 and 122. Thus this will include everything from 0 to 9, Capital A to Z and small case a to z. --->

<cfset a = randrange(48,122)>

<!--- This is where the stucture is populated, Conceptually, ststring is a new structure specified above....ststring1 is key 1, ststring2 is key 2, ststring3 is key 3, and so on --->

<!--- since all characters between 57 and 65 and those between 90 and 97 are charachters like "[,],+,=,..,$,#" and so on, we don't need them. Unless ofcourse yours is a password with special characters. That explains the condition below. If our randrange givers a number like 92, we don't need it and I chose to subtitue it with an "E"...you can use your own substitution. --->

<cfif (#a# gt 57 and #a# lt 65) or (#a# gt 90 and #a# lt 97)>
<cfset ststring["#i#"]="E">
<cfelse>
<cfset ststring["#i#"]=#chr(a)#>
</cfif>

</cfloop>


<!--- Now that our password is made up of 6 stings, it is just a question of putting that together, thus ... --->

<cfset stpassword ="#ststring[1]##ststring[2]##ststring[3]##ststring[4]##ststring[5]##ststring[6]#">


<!--- In principle, take each variable in a string of six, and assign a character to it which falls between 48 to 57 or 65 to 90 or 97 to 122 --->

Randomly Generated Password : <b><cfoutput>#stpassword#</cfoutput></b>

<!--- let me know if you think of any improvements on this one --->

No comments: