Generate Complex Password

//complex input boolean
if (complex == true) {
 var charSets = [
  { minAmount: 1,
    chars: "abcdefghjkmnpqrstuvwxyz"
  },
  {
    minAmount: 1,
    chars: "1234567890"
  },
  {
    minAmount: 1,
    chars: "ABCDEFGHJKMNPQRSTUVXYZ"
  },
     {
    minAmount: 1,
    chars: '!@#%^*'
  }
 ]

}else {
 var charSets = [
  { minAmount: 1,
    chars: "abcdefghjkmnpqrstuvwxyz"
  },
  {
    minAmount: 1,
    chars: "1234567890"
  },
  {
    minAmount: 1,
    chars: "ABCDEFGHJKMNPQRSTUVXYZ"
  }
 ]

}

pwLength = 12;
password = new Array();

function getRandomInt(max) {
 return Math.floor(Math.random()*max);
}

//make sure the minimum amount for each char set is met.
charSets.forEach(function(charSet) {
 for (i = 0; i < charSet.minAmount; i++) {
  var posFound = false
  var position
  while (!posFound)  { //check if random choosen position is not already taken. If so, generate a new position
   position = getRandomInt(pwLength);
   posFound = !(password[position])   
  }
  password[position] = charSet.chars.charAt(getRandomInt(charSet.chars.length));
 }
});

//fill the remainder of the pw
for (i = 0; i < pwLength; i++) {
 if (!(password[i])) { //check if position is allready filled
  charSet = charSets[getRandomInt(charSets.length)]; //randomly select a charset.
  password[i] = charSet.chars.charAt(getRandomInt(charSet.chars.length)); //randomly select a char from the char set.
 }
}

return password.join("");
Post a Comment (0)
Previous Post Next Post