Skip to content

Instantly share code, notes, and snippets.

@shaunthomas999
Last active July 14, 2016 18:08
Show Gist options
  • Save shaunthomas999/83cea7835351b1a56dab2fb9a5a15e24 to your computer and use it in GitHub Desktop.
Save shaunthomas999/83cea7835351b1a56dab2fb9a5a15e24 to your computer and use it in GitHub Desktop.
Javascript code sample. Disclaimer: This algorithem is not the best and optimised.
/*
* Password Generator
*
* @author Shaun Thomas
*
* Objective
* --
* \_ Generate 12 character length password from domain name
* \_ Re-creatable by hand without much complexity
* \_ Password has decent level of randomness
*/
console.log("*\n* Password Generator \n*");
/*
* Step 1: Enter domain name
*/
var domainName = "google";
/*
* Step 2: Take first 7 characters (customizable) of the domain name.
* If the domain has less characters then append 0’s
*/
var DOMAIN_NAME_LENGTH_TO_CONSIDER = 7;
var password;
if(domainName.length >= DOMAIN_NAME_LENGTH_TO_CONSIDER) {
password = domainName.substr(0, DOMAIN_NAME_LENGTH_TO_CONSIDER);
}
else {
password = domainName;
while(password.length < DOMAIN_NAME_LENGTH_TO_CONSIDER) {
password = password.concat("0");
}
}
console.log("First password phrase: "+ password);
/*
* Step 3: Choose a 3 digit random number (Remember this random number. Can be used across different domains)
*/
var firstRandomNumberValue = 152;
var firstRandomNumber = firstRandomNumberValue.toString();
/*
* Step 4: Insert the above random number into the first password phrase at positions 3, 5 and 7
*/
var numberInsertPositions = [3, 5, 7];
for(var idx=0 ; idx < numberInsertPositions.length ; idx++) {
var pwArray = [];
pwArray[0] = password.slice(0, numberInsertPositions[idx] - 1);
pwArray[0] = pwArray[0].concat(firstRandomNumber.charAt(idx));
pwArray[1] = password.slice(numberInsertPositions[idx] - 1);
password = pwArray.join('');
}
console.log("Second password phrase: "+ password);
/*
* Step 5: Choose another 3 digit random number (Remember this random number. Can be used across different domains)
*/
var secondRandomNumberValue = 765;
var secondRandomNumber = secondRandomNumberValue.toString();
/*
* Step 6: Add the digits (from left to right) in the random number above with the character code of characters at
* position 2, 4 and 6 of the second password phrase
*/
var characterIncrementPositions = [2, 4, 6];
for(var idx = 0; idx < characterIncrementPositions.length ; idx++) {
var characterIncrementIndex = characterIncrementPositions[idx] - 1;
var characterToIncrement = password.charAt(characterIncrementIndex);
var characterToInsert;
// If the character is 0 just consider the digit to insert
if(characterToIncrement === "0") {
characterToInsert = secondRandomNumber.charAt(idx)
}
else {
var charCodeOfNewCharToInsert = characterToIncrement.charCodeAt(0) + parseInt(secondRandomNumber.charAt(idx));
// If charCodeOfNewCharToInsert is greater than the range of English letters then start again from 'a' after 'z'
if(charCodeOfNewCharToInsert - 97 > 26) {
charCodeOfNewCharToInsert -= 26;
}
characterToInsert = String.fromCharCode(charCodeOfNewCharToInsert);
}
password = [ password.slice(0, characterIncrementIndex),
characterToInsert,
password.slice(characterIncrementIndex + 1)].join('');
}
console.log("Third password phrase: "+ password);
/*
* Step 7: Prepend four special characters to the third password phrase to make the password
*/
var specialCharactersToPrepend = "$#+!";
password = specialCharactersToPrepend + password;
console.log("*\n* Final password: "+ password + "\n*");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment