Commit 3ed1d775 authored by John Crepezzi's avatar John Crepezzi Committed by GitHub
Browse files

Merge pull request #158 from KlasafGeijerstam/master

Added dictionary key generator
parents 5a8697cd 87b1c76a
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
var fs = require('fs');

var DictionaryGenerator = function(options) {
  //Options
  if (!options) 
    return done(Error('No options passed to generator'));
  if (!options.path)
    return done(Error('No dictionary path specified in options'));
  
  //Load dictionary
  fs.readFile(options.path, 'utf8', (err,data) => {
    if (err) throw err;
    this.dictionary = data.split(/[\n\r]+/);
  });
};

//Generates a dictionary-based key, of keyLength words
DictionaryGenerator.prototype.createKey = function(keyLength) {
  var text = '';
  for(var i = 0; i < keyLength; i++)
    text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)];

  return text;
};

module.exports = DictionaryGenerator;