CFLib.org – Common Function Library Project

generatePassword([numberOfCharacters] [, characterFilter])

Last updated February 8, 2010

Version: 2 | Requires: ColdFusion 5 | Library: SecurityLib

 
Rated 5 time(s). Average Rating: 3.0

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

Return Values:
Returns a string.

Example:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
createPrimeNumbe...
3 day(s) ago

Ray Ford Ray Ford added
timeZoneNow
17 day(s) ago

Henry Ho Henry Ho added
queryExecute
27 day(s) ago

Rick Root Rick Root added
deleteDirectory
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 47 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson