2017-06-26 15:17:52 +00:00
|
|
|
var rand = require('random-js');
|
|
|
|
var fs = require('fs')
|
|
|
|
var dictionary;
|
2017-06-26 15:37:04 +00:00
|
|
|
var randomEngine = rand.engines.nativeMath;
|
2017-06-26 15:17:52 +00:00
|
|
|
var random;
|
|
|
|
|
|
|
|
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
|
2017-06-26 15:37:04 +00:00
|
|
|
fs.readFile(options.path, 'utf8', (err,data) => {
|
2017-06-26 15:17:52 +00:00
|
|
|
if(err) throw err;
|
2017-06-26 15:37:04 +00:00
|
|
|
this.dictionary = data.split(',');
|
2017-06-26 15:17:52 +00:00
|
|
|
|
|
|
|
//Remove any non alpha-numeric characters
|
2017-06-26 15:37:04 +00:00
|
|
|
for(var i = 0; i < this.dictionary.length; i++)
|
|
|
|
this.dictionary[i] = this.dictionary[i].replace(/\W/g,'');
|
|
|
|
|
|
|
|
this.random = rand.integer(0, this.dictionary.length);
|
2017-06-26 15:17:52 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
//Generates a dictionary-based key, of keyLength words
|
|
|
|
DictionaryGenerator.prototype.createKey = function(keyLength) {
|
|
|
|
var text = '';
|
|
|
|
for(var i = 0; i < keyLength; i++)
|
2017-06-26 15:37:04 +00:00
|
|
|
text += this.dictionary[random(randomEngine)];
|
2017-06-26 15:17:52 +00:00
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = DictionaryGenerator;
|