2017-11-01 01:55:59 +01:00
|
|
|
const fs = require('fs');
|
2017-06-26 17:17:52 +02:00
|
|
|
|
2017-11-01 01:55:59 +01:00
|
|
|
module.exports = class DictionaryGenerator {
|
2017-06-26 18:03:18 +02:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
constructor(options, readyCallback){
|
|
|
|
// Check options format
|
|
|
|
if (!options) throw Error('No options passed to generator');
|
|
|
|
if (!options.path) throw Error('No dictionary path specified in options');
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
// Load dictionary
|
|
|
|
fs.readFile(options.path, 'utf8', (err, data) => {
|
|
|
|
if (err) throw err;
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
this.dictionary = data.split(/[\n\r]+/);
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
if (readyCallback) readyCallback();
|
|
|
|
});
|
|
|
|
}
|
2017-06-26 17:17:52 +02:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
// Generates a dictionary-based key, of keyLength words
|
|
|
|
createKey(keyLength){
|
|
|
|
let text = '';
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
for (let i = 0; i < keyLength; i++){
|
|
|
|
const index = Math.floor(Math.random() * this.dictionary.length);
|
|
|
|
text += this.dictionary[index];
|
|
|
|
}
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-28 04:39:03 +02:00
|
|
|
return text;
|
|
|
|
}
|
2017-11-01 01:55:59 +01:00
|
|
|
|
|
|
|
};
|