2020-08-29 00:40:36 +02:00
|
|
|
const winston = require('winston');
|
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-29 00:40:36 +02:00
|
|
|
constructor({ path } = {}, readyCallback){
|
|
|
|
//check for dictionary path
|
|
|
|
if (!path){
|
|
|
|
winston.error('No dictionary path specified in options');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-29 00:40:36 +02:00
|
|
|
//load dictionary
|
|
|
|
if (!fs.existsSync(path)){
|
|
|
|
winston.error(`Dictionary file "${path}" doesn't exist`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-29 00:40:36 +02:00
|
|
|
this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm);
|
|
|
|
if (readyCallback) readyCallback();
|
2020-08-28 04:39:03 +02:00
|
|
|
}
|
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){
|
2020-08-29 00:40:36 +02:00
|
|
|
return Array(keyLength).fill().map(() => this.dictionary[ Math.floor(Math.random() * this.dictionary.length) ] ).join('');
|
2020-08-28 04:39:03 +02:00
|
|
|
}
|
2017-11-01 01:55:59 +01:00
|
|
|
|
|
|
|
};
|