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-09-23 01:07:32 +02:00
|
|
|
constructor({ path } = {}){
|
2020-08-29 00:40:36 +02:00
|
|
|
//check for dictionary path
|
|
|
|
if (!path){
|
2020-09-23 01:07:32 +02:00
|
|
|
let error = 'No dictionary path specified in options';
|
|
|
|
winston.error(error);
|
|
|
|
throw error;
|
2020-08-29 00:40:36 +02:00
|
|
|
}
|
2017-11-01 01:55:59 +01:00
|
|
|
|
2020-08-29 00:40:36 +02:00
|
|
|
//load dictionary
|
|
|
|
if (!fs.existsSync(path)){
|
2020-09-23 01:07:32 +02:00
|
|
|
let error = `Dictionary file "${path}" doesn't exist`;
|
|
|
|
winston.error(error);
|
|
|
|
throw error;
|
2020-08-29 00:40:36 +02:00
|
|
|
}
|
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);
|
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
|
|
|
|
|
|
|
};
|