Commit 80f06187 authored by Klas af Geijerstam's avatar Klas af Geijerstam Committed by GitHub
Browse files

Updated dictionary.js

Now expects a newline separated dictionary, supports both \n and \n\r
parent ac2bceef
Loading
Loading
Loading
Loading
+7 −9
Original line number Diff line number Diff line
@@ -11,20 +11,18 @@ var DictionaryGenerator = function(options) {
  //Load dictionary
  fs.readFile(options.path, 'utf8', (err,data) => {
    if(err) throw err;
    this.dictionary = data.split(',');
    
    //Remove any non alpha-numeric characters
    for(var i = 0; i < this.dictionary.length; i++)
      this.dictionary[i] = this.dictionary[i].replace(/\W/g,'');
    
    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)];
  for(var i = 0; i < keyLength; i++) {
    var index =Math.floor(Math.random()*this.dictionary.length);
    text += this.dictionary[index];
  }

  return text;
};