Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active January 2, 2018 02:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branneman/d315eef78dbdbb455b85aec6dd81047f to your computer and use it in GitHub Desktop.
Save branneman/d315eef78dbdbb455b85aec6dd81047f to your computer and use it in GitHub Desktop.
[FP playground] Generate diceware passwords with NodeJS. See also: https://www.rempe.us/diceware/
const map = fn => list => list.map(fn);
const filter = fn => list => list.filter(fn);
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
const tail = list => list.slice(1);
const fill = val => num => new Array(num).fill(val);
const getFileContents = file => require('fs').readFileSync(file, { encoding: 'utf8' });
const split = char => str => str.split(char);
const join = char => list => list.join(char);
const isWordLine = str => /[1-6]{5}\t.+/.test(str);
const getWordList = compose(map(split('\t')), filter(isWordLine), split('\n'), getFileContents);
const wordList = getWordList('eff.wordlist.txt');
const roll1D6 = () => Math.round((Math.random() * 5) + 1);
const roll5D6 = () => map(roll1D6)(fill(0)(5));
const getWord = n => wordList.find(i => i[0] === n);
const getRandomWord = compose(join(' '), tail, getWord, join(''), roll5D6);
const getRandomWords = compose(join(' '), map(getRandomWord), fill(0));
const app = len => compose(join('\n'), map(() => getRandomWords(len)), fill(0));
//
// App
//
const numberOfWordsInPassword = 8;
const numberOfPasswords = 25;
const out = app(numberOfWordsInPassword)(numberOfPasswords);
console.log(out);
11111 abacus
11112 abdomen
11113 abdominal
11114 abide
11115 abiding
11116 ability
11121 ablaze
11122 able
11123 abnormal
11124 abrasion
11125 abrasive
11126 abreast
11131 abridge
11132 abroad
...
See: https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment