Skip to content

Instantly share code, notes, and snippets.

@unicornist
Forked from smhnaji/generatePassword.php
Last active July 2, 2016 12:03
Show Gist options
  • Save unicornist/70579ed24d0334bce2e0ac2b1ecf0a3d to your computer and use it in GitHub Desktop.
Save unicornist/70579ed24d0334bce2e0ac2b1ecf0a3d to your computer and use it in GitHub Desktop.
Generate secure and readable password
// just translated from the php version (I have to optimise it)
function generatePassword (length) {
if(!length) length = 8;
var sets = ['abcdefghjkmnpqrstuvwxyz', 'ABCDEFGHJKMNPQRSTUVWXYZ', '23456789', '~!@#$^*()_.-=+'];
var all = '';
var password = '';
sets.forEach(function(set){
set = shuffle(set);
password += set[0];
all += set;
});
for(var i=0; i<length-sets.length; i++) {
all = shuffle(all);
password += all[0];
}
password = shuffle(password);
return password; //'Uc74T?y$'
}
function shuffle (str) {
var a = str.split(""), n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
@unicornist
Copy link
Author

just translated from the php version (I have to optimise it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment