Skip to content

Instantly share code, notes, and snippets.

@keithics
Last active January 4, 2020 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithics/e38ce82647b7f684d70c26e4a126fd38 to your computer and use it in GitHub Desktop.
Save keithics/e38ce82647b7f684d70c26e4a126fd38 to your computer and use it in GitHub Desktop.
Generate Strong Password - Angular (minimum of 4 chars - random string with 1 uppercase, 1 number and 1 special character)
'use strict';
angular.module('users').factory('PasswordGenerator', function () {
return {
generate: function (length, useUpper, useNumbers, userSymbols) {
var passwordLength = length || 12;
var addUpper = useUpper || true;
var addNumbers = useNumbers || true;
var addSymbols = userSymbols || true;
var lowerCharacters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var upperCharacters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var symbols = ['!', '?', '@'];
var getRandom = function (array) {
return array[Math.floor(Math.random() * array.length)];
}
var finalCharacters = "";
if (addUpper) {
finalCharacters = finalCharacters.concat(getRandom(upperCharacters));
}
if (addNumbers) {
finalCharacters = finalCharacters.concat(getRandom(numbers));
}
if (addSymbols) {
finalCharacters = finalCharacters.concat(getRandom(symbols));
}
for (var i = 1; i < passwordLength - 3; i++) {
finalCharacters = finalCharacters.concat(getRandom(lowerCharacters));
}
//shuffle!
return finalCharacters.split('').sort(function () {
return 0.5 - Math.random()
}).join('');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment