Last active
June 4, 2019 16:31
-
-
Save tanerochris/1eece64e01a8d0d783bfeab7d37b3a50 to your computer and use it in GitHub Desktop.
code snippet to generate password
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
/** | |
* Function | |
* @param {Number} seed length of sequence of characters generated | |
* @returns {String} sequence of Capital letters | |
*/ | |
let generateCaps = (seed) => { | |
// randaomly select letters withing ascii characters | |
var string = ''; | |
for (let i = 0; i <= seed; i++) { | |
// select randomNumber | |
var capLetter = Math.floor(Math.random() * 26) + 65; | |
// convert number to caps | |
string += String.fromCharCode(capLetter); | |
} | |
return string; | |
}; | |
/** | |
* Function generates a sequence of characters | |
* @param {Number} seed length of sequence of characters generated | |
* @returns {String} a sequence of numbers | |
*/ | |
let generateNums = (seed) => { | |
var string = ''; | |
for (let i = 0; i <= seed; i++) { | |
var number = Math.floor(Math.random() * 10); | |
string += number; | |
} | |
return string; | |
}; | |
/** | |
* Function generates a sequence of characters | |
* @returns {String} returns a sequence of special characters. | |
*/ | |
let generateSpecials = () => { | |
var arraySpecial = ['@', '#', '$', '!']; | |
var index = Math.floor(Math.random() * 4); | |
return arraySpecial[index]; | |
}; | |
/** | |
* Generates a sequence of characters | |
* @param {Number} seed lenght of characters to generatetr | |
* @returns {String} returns a sequence of ascii characters | |
*/ | |
let generateAny = (seed) => { | |
// randaomly select letters withing ascii characters | |
var string = ''; | |
for (let i = 0; i <= seed; i++) { | |
// select randomNumber | |
var capLetter = Math.floor(Math.random() * 25) + 97; | |
// convert number to caps | |
string += String.fromCharCode(capLetter); | |
} | |
return string; | |
}; | |
/** | |
* Function to generate password | |
* @param {Number} minPassLength minimum passwordLength to generate | |
* @param {Array} charcterGenerators array containing funtions that return a sequence of characters | |
*/ | |
let generatePassword = (minPassLength, charcterGenerators) => { | |
let functions = charcterGenerators; | |
let alreadyRan = []; | |
let randomString = ''; | |
// your can add or more generators or pass in pattern as arguments | |
// let pattern = /yourpatter/ | |
while (1) { | |
/* pattern test should replace allFunctionsDidRun | |
* allFunctionsDidRun is used because i could not figure the right regex for | |
* atleast one cap, atleast one num, atleast one special character and atleast a letter , | |
* all of the above appearing in the string in any order. | |
*/ | |
if (randomString.length >= minPassLength && allFunctionsDidRun(alreadyRan, functions.length)) | |
break; | |
let selectedIndex = Math.floor(Math.random() * functions.length); | |
if (alreadyRan.indexOf('' + selectedIndex + '') >= 0) { | |
if (allFunctionsDidRun(alreadyRan)) | |
alreadyRan = []; | |
continue; | |
} else { | |
// generate another random string | |
let seed = Math.floor(Math.random() * 3); | |
// call functions and pass arguments | |
randomString += functions[selectedIndex](seed); | |
alreadyRan.push('' + selectedIndex + ''); | |
} | |
} | |
/** | |
* best to use regex ,there exist a possibility that the first 20 characters might not match | |
* after a 100 run of the function, the first 20 characters matched pattern but we cannot rely on that. | |
* TODO : Opimise function | |
*/ | |
if(randomString.length > 20) | |
return randomString.substring(0,21); | |
return randomString; | |
} | |
/** Checks if all functions in an array of functions were called | |
* @param {Array} alreadyRan contains indices of already ran character generators | |
* @param {Number} charcterGeneratorsLength the length of character generators available | |
*/ | |
let allFunctionsDidRun = ( alreadyRan, characterGeneratorsLength) => { | |
let allRan = true; | |
for(let i = 0 ; i < characterGeneratorsLength; i++){ | |
if(alreadyRan.indexOf('' + i + '') == -1){ | |
allRan = false; | |
break; | |
} | |
} | |
return allRan; | |
} | |
console.log(generatePassword(10, [generateCaps, generateNums, generateSpecials, generateAny])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment