Created
June 25, 2020 07:34
-
-
Save natchiketa/efd1569f4e525809c8e20984791b563d to your computer and use it in GitHub Desktop.
generate strong password in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var specials = '!@#$%^&*()_+{}:"<>?\|[];\',./`~'; | |
var lowercase = 'abcdefghijklmnopqrstuvwxyz'; | |
var uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
var numbers = '0123456789'; | |
var all = specials + lowercase + uppercase + numbers; | |
String.prototype.pick = function(min, max) { | |
var n, chars = ''; | |
if (typeof max === 'undefined') { | |
n = min; | |
} else { | |
n = min + Math.floor(Math.random() * (max - min)); | |
} | |
for (var i = 0; i < n; i++) { | |
chars += this.charAt(Math.floor(Math.random() * this.length)); | |
} | |
return chars; | |
}; | |
// Credit to @Christoph: http://stackoverflow.com/a/962890/464744 | |
String.prototype.shuffle = function() { | |
var array = this.split(''); | |
var tmp, current, top = array.length; | |
if (top) while (--top) { | |
current = Math.floor(Math.random() * (top + 1)); | |
tmp = array[current]; | |
array[current] = array[top]; | |
array[top] = tmp; | |
} | |
return array.join(''); | |
}; | |
var password = (specials.pick(1) + lowercase.pick(1) + uppercase.pick(1) + all.pick(3, 10)).shuffle(); | |
alert(password); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment