mirror of
https://github.com/privacyguides/privacyguides.org.git
synced 2024-10-01 01:35:57 -04:00
modularized random number generation logic
This commit is contained in:
parent
b38eadd4ed
commit
e13220c2fb
@ -1,29 +1,35 @@
|
|||||||
var passwordGenerator = (function() {
|
var passwordGenerator = (function() {
|
||||||
return {
|
var generateRandomNum = function (max) {
|
||||||
generatePassword: function (options) {
|
return Math.floor(Math.random() * max);
|
||||||
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
|
};
|
||||||
var lowercase = "abcdefghiklmnopqrstuvwxyz";
|
|
||||||
var numbers = "0123456789";
|
var generatePassword = function (options) {
|
||||||
var punct = ".,-/#!$%^&*;:{}=-_`~()]";
|
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
|
||||||
var candidates = '';
|
var lowercase = "abcdefghiklmnopqrstuvwxyz";
|
||||||
if (options.includeUppercaseChars) {
|
var numbers = "0123456789";
|
||||||
candidates += uppercase;
|
var punct = ".,-/#!$%^&*;:{}=-_`~()]";
|
||||||
}
|
var candidates = '';
|
||||||
if (options.includeLowercaseChars) {
|
if (options.includeUppercaseChars) {
|
||||||
candidates += lowercase;
|
candidates += uppercase;
|
||||||
}
|
|
||||||
if (options.includeNumbers) {
|
|
||||||
candidates += numbers;
|
|
||||||
}
|
|
||||||
if (options.includePunctuationChars) {
|
|
||||||
candidates += punct;
|
|
||||||
}
|
|
||||||
var result = "";
|
|
||||||
for (var i = 0; i < options.passwordLength; i++) {
|
|
||||||
var randomNum = Math.floor(Math.random() * candidates.length);
|
|
||||||
result += candidates.substring(randomNum, randomNum + 1);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
if (options.includeLowercaseChars) {
|
||||||
|
candidates += lowercase;
|
||||||
|
}
|
||||||
|
if (options.includeNumbers) {
|
||||||
|
candidates += numbers;
|
||||||
|
}
|
||||||
|
if (options.includePunctuationChars) {
|
||||||
|
candidates += punct;
|
||||||
|
}
|
||||||
|
var result = "";
|
||||||
|
for (var i = 0; i < options.passwordLength; i++) {
|
||||||
|
var randomNum = generateRandomNum(candidates.length);
|
||||||
|
result += candidates.substring(randomNum, randomNum + 1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
generatePassword: generatePassword
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
Loading…
Reference in New Issue
Block a user