This site uses third-party cookies, learn more or accept
dark light

Password Generator #2

Written by Max Pelic on

After making this password generator, I decided to make another one that used a list of words to create a random combination of words for a password.

I finished it last night, and you can view it at maxpelic.com/examples/password_word_generator.html.

Compression

I started by creating a Python script that compressed the list of words I planned on using. First, it groups words by the first two letters and creates sections with them, separated by capital letters.

For example, the words total, toenail, today, and topical would become toTalEnailDayPical. the first two letters are the common letters all the words share, and the rest of the letters are combinations that can be added to create a real word.

Next, I combined all the sections, separated by a ., and replaced the most common letter combinations with various symbols. For example, toTalEnailPical might become toT?EnailPic?.

Then I printed out the symbols, replacements, and compressed string to place in my HTML file. Here's an example of the first part of the string: abAonIl@yOr!OO^RoadSen]Su:Su: S>b... (I removed some characters that weren't showing up right...)

Decompression

To decompress the list, I basically do the same process in reverse. Here's a basic implementation of the decompression:

1function decodeSection(section){

2 let start = section.substr(0, 2), list = [];
3 section = section.substr(2).split(/(?!^)(?=[A-Z])/g);
4 for(let i = 0; i < section.length; i++){
5 list.push((start + section[i]).toLowerCase());
6 }
7 return list;
8}
9function getDecodedWordList(){
10 let wordlist = getWordList(), i, words=[];
11 const to = ['tion','atio','sion','ionN','ionS','ionT','ionP','ionO','ionR'], from = '123456789';
12 for(i=0;i<from.length;i++)
13 while(wordlist.indexOf(from[i]) > -1)
14 wordlist = wordlist.replace(from[i], to[i]);
15 wordlist = wordlist.split('.');
16 for(i=0;i<wordlist.length;i++)
17 words = words.concat(decodeSection(wordlist[i]));
18 return words;
19}

Picking words

From there, it's pretty easy to pick words. I used window.crypto, a cryptographically secure way to generate random numbers. Assuming window.words contains the full list of words, this is how I generated random words:

1let randomNumbers = new Uint32Array(3);

2window.crypto.getRandomValues(randomNumbers);
3let newWords = [];
4for(let i = 0; i < 3; i++){
5 newWords.push(window.words[randomNumbers[i] % window.words.length]);
6}
7console.log(randomWords);

Learn more

Feel free to look under the hood of the password generator and email me with any questions or suggestions you have (max@maxpelic.com)

Share this article:

Previous Article: PHP File Directory Layout Best Practices

Next Article: CSS units of mesaurement