Merge pull request #158 from KlasafGeijerstam/master
Added dictionary key generator
This commit is contained in:
commit
3ed1d775ac
1 changed files with 26 additions and 0 deletions
26
lib/key_generators/dictionary.js
Normal file
26
lib/key_generators/dictionary.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
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;
|
Loading…
Reference in a new issue