Skip to content

Instantly share code, notes, and snippets.

@Joaquim3
Created August 3, 2024 10:58
Show Gist options
  • Select an option

  • Save Joaquim3/b7d6964276b1134d57fb85de4335024f to your computer and use it in GitHub Desktop.

Select an option

Save Joaquim3/b7d6964276b1134d57fb85de4335024f to your computer and use it in GitHub Desktop.
JAVASCRIPT ⇢ generate a password with 11 characters
//--------------------------------------------------
// purpose : generate a password with 11 characters
// return a string
//--------------------------------------------------
function fncGeneratePwd() {
const sCharSymbols = "+=?!@#$%*"; // you can add more symbols
const sCharDigits = "0123456789";
const sCharUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const sCharLower = "abcdefghijklmnopqrstuvwxyz";
var sRetval = "";
for (let i = 1; i < 5; i++) {
switch(i) {
// in this case, the total amount of chars below is : 2 + 2 + 2 + 5 = 11
// you can change this or you can random them, in order to force security.
// you can also add more than 11 chars
case 1: sPos=9; sNbr=2; sCodes = sCharSymbols; break; // have 2 symbols
case 2: sPos=10; sNbr=2; sCodes = sCharDigits; break; // have 2 digits
case 3: sPos=26; sNbr=2; sCodes = sCharUpper; break; // have 4 upper case chars
case 4: sPos=26; sNbr=5; sCodes = sCharLower; break; // have 5 lower case chars
}
for (let k = 0; k < sNbr; k++) {
sRetval += sCodes.charAt(Math.floor(Math.random(1) * (sPos)));
}
}
// this replace all characters positions in the string randomly
var sRetval = sRetval.split('').sort(function(){return 0.5-Math.random()}).join('');
return sRetval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment