5 Dec 2011

Generating random password in Coldfusion 9

This coldfusion code can create a random password with rules.

"border:
 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; 
font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: 
rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; 
line-height: 14px; width: 100%;">
/**         Generates a random password using set of characters         @output false     **/     public string function getRandomPassword()     {         // Set up available lower case values.         var strLowerCaseAlpha = "abcdefghijklmnopqrstuvwxyz";                   // Set up available upper case values.         var strUpperCaseAlpha = UCase( strLowerCaseAlpha );                   // Set up available numbers.         var strNumbers = "0123456789";                   // Set up additional valid password chars.         var strOtherChars = "~!@##$%^&*";                  var strAllValidChars = (strLowerCaseAlpha & strUpperCaseAlpha & strNumbers & strOtherChars);                  // Create an array to contain the password ( think of a string as an array of character).         var arrPassword = ArrayNew( 1 );                  /**         Password         - must be exactly 8 characters in length         - must have at least 1 number         - must have at least 1 uppercase letter         - must have at least 1 lower case letter         **/                  // Select the random number from our number set.         arrPassword[ 1 ] = Mid(strNumbers,RandRange( 1, Len( strNumbers ) ),1);                   // Select the random letter from our lower case set.         arrPassword[ 2 ] = Mid(strLowerCaseAlpha,RandRange( 1, Len( strLowerCaseAlpha ) ),1);                   // Select the random letter from our upper case set.         arrPassword[ 3 ] = Mid(strUpperCaseAlpha,RandRange( 1, Len( strUpperCaseAlpha ) ),1);                  // Create rest of the password.         for (intChar = (ArrayLen( arrPassword ) + 1); intChar LTE 8; intChar ++)         {             //Pick random value.             arrPassword[ intChar ] = Mid(strAllValidChars,RandRange( 1, Len( strAllValidChars ) ),1);         }                  // Uses the Java Collections utility class to shuffle the array into a "random" order.         CreateObject( "java", "java.util.Collections" ).Shuffle(arrPassword);                             // Join all the characters into a single string.         strPassword = ArrayToList(arrPassword,"");                  return strPassword;     }

0 comments:

Post a Comment