1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-01 09:40:21 +01:00
haste-server/lib/key_generators/dictionary.js

30 lines
724 B
JavaScript
Raw Normal View History

const winston = require('winston');
const fs = require('fs');
module.exports = class DictionaryGenerator {
constructor({ path } = {}){
//check for dictionary path
if (!path){
let error = 'No dictionary path specified in options';
winston.error(error);
throw error;
}
//load dictionary
if (!fs.existsSync(path)){
let error = `Dictionary file "${path}" doesn't exist`;
winston.error(error);
throw error;
}
this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm);
2020-08-28 04:39:03 +02:00
}
2020-08-28 04:39:03 +02:00
// Generates a dictionary-based key, of keyLength words
createKey(keyLength){
return Array(keyLength).fill().map(() => this.dictionary[ Math.floor(Math.random() * this.dictionary.length) ] ).join('');
2020-08-28 04:39:03 +02:00
}
};