CFLib.org – Common Function Library Project

generatePassword([numberOfCharacters][, characterFilter])

Last updated February 09, 2010

author

Tony Blackmon

Version: 2 | Requires: CF5 | Library: SecurityLib

Description:
Generates a decent password that can contain uppercase, lowercase, numeric, and punctuation characters.

Return Values:
Returns a string.

Example:

<cfset newPassword = generatePassword(8)>
<cfoutput>
   #newPassword#
</cfoutput>

Parameters:

Name Description Required
numberOfCharacters Lengh for the generated password. Defaults to 8. No
characterFilter Characters filtered from result. Defaults to O,o,0,i,l,1,I,5,S No

Full UDF Source:

/**
 * Generates a password the length you specify.
 * v2 by James Moberg.
 * 
 * @param numberOfCharacters      Lengh for the generated password. Defaults to 8. (Optional)
 * @param characterFilter      Characters filtered from result. Defaults to O,o,0,i,l,1,I,5,S (Optional)
 * @return Returns a string. 
 * @author Tony Blackmon (fluid@sc.rr.com) 
 * @version 2, February 8, 2010 
 */
function generatePassword() {
var placeCharacter = "";
var currentPlace=0;
var group=0;
var subGroup=0;
var numberofCharacters = 8;
var characterFilter = 'O,o,0,i,l,1,I,5,S';
var characterReplace = repeatString(",", listlen(characterFilter)-1);
if(arrayLen(arguments) gte 1) numberofCharacters = val(arguments[1]);
if(arrayLen(arguments) gte 2) {
characterFilter = listsort(rereplace(arguments[2], "([[:alnum:]])", "\1,", "all"),"textnocase");
characterReplace = repeatString(",", listlen(characterFilter)-1);
}
while (len(placeCharacter) LT numberofCharacters) {
group = randRange(1,4, 'SHA1PRNG');
switch(group) {
case "1":
subGroup = rand();
    switch(subGroup) {
case "0":
placeCharacter = placeCharacter & chr(randRange(33,46, 'SHA1PRNG'));
break;
case "1":
placeCharacter = placeCharacter & chr(randRange(58,64, 'SHA1PRNG'));
break;
}
case "2":
placeCharacter = placeCharacter & chr(randRange(97,122, 'SHA1PRNG'));
break;
case "3":
placeCharacter = placeCharacter & chr(randRange(65,90, 'SHA1PRNG'));
break;
case "4":
placeCharacter = placeCharacter & chr(randRange(48,57, 'SHA1PRNG'));
break;
}
if (listLen(characterFilter)) {
placeCharacter = replacelist(placeCharacter, characterFilter, characterReplace);
}
}
return placeCharacter;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson