CFLib.org – Common Function Library Project

generatePassword([numberOfCharacters][, characterFilter])

Last updated February 9, 2010
Download UDF

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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Mosh Teitelbaum added
minutesToUtcOffs...
24 days ago

Mosh Teitelbaum added
utcOffsetToMinut...
24 days ago

Hank van Empel added
validateIBAN
25 days ago

CF Ninja added
calcIRR
3 months ago

Stephen Withington added
getWeekOfMonth
3 months ago

Created by Raymond Camden / Design by Justin Johnson