2017-06-26 18:10:57 +02:00
|
|
|
var fs = require('fs');
|
2017-06-26 17:17:52 +02:00
|
|
|
|
|
|
|
var DictionaryGenerator = function(options) {
|
|
|
|
//Options
|
2017-06-26 18:19:36 +02:00
|
|
|
if (!options) throw Error('No options passed to generator');
|
|
|
|
if (!options.path) throw Error('No dictionary path specified in options');
|
|
|
|
|
2017-06-26 17:17:52 +02:00
|
|
|
//Load dictionary
|
2017-06-26 18:19:36 +02:00
|
|
|
fs.readFile(options.path, 'utf8', (err, data) => {
|
2017-06-26 18:10:57 +02:00
|
|
|
if (err) throw err;
|
2017-06-26 18:03:18 +02:00
|
|
|
this.dictionary = data.split(/[\n\r]+/);
|
|
|
|
});
|
2017-06-26 17:17:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//Generates a dictionary-based key, of keyLength words
|
|
|
|
DictionaryGenerator.prototype.createKey = function(keyLength) {
|
|
|
|
var text = '';
|
2017-06-26 18:09:13 +02:00
|
|
|
for(var i = 0; i < keyLength; i++)
|
2017-06-26 18:10:57 +02:00
|
|
|
text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)];
|
2017-06-26 18:03:18 +02:00
|
|
|
|
2017-06-26 17:17:52 +02:00
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = DictionaryGenerator;
|