Skip to content

Instantly share code, notes, and snippets.

@isu3ru
Created December 6, 2018 17:31
Show Gist options
  • Save isu3ru/0800cd5e9fbfd76999f267cdffd53a17 to your computer and use it in GitHub Desktop.
Save isu3ru/0800cd5e9fbfd76999f267cdffd53a17 to your computer and use it in GitHub Desktop.
Password generator function in javascript
function generate(size) {
let config = {
numbers: true,
symbols: false
};
// add symbols if needed
// config.symbols = true;
let alphabet_low = "abcdefghijklmnopqrstuvwxyz"
let alphabet_up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let numbers = "0123456789";
let symbols = "~!@#$%^&*()_+=-;:{}[]<>?/";
var availChars = alphabet_low + alphabet_up; // can't TURN OFF the alphabet
if (config.numbers) {
availChars = alphabet_low + numbers + alphabet_up;
}
if (config.symbols) {
availChars += symbols;
}
// A reasonably "intuitive" way to generate a password:
var charPos;
var pwChar;
var pwLength = 10;  // Change for shorter or longer password
var pw = "";
for (i = 0; i < pwLength; i++) {
charPos = Math.floor(Math.random() * availChars.length);
pwChar = availChars.charAt(charPos);
pw = pw + pwChar;
}
return pw;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment