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

33 lines
738 B
JavaScript
Raw Normal View History

const fs = require('fs');
module.exports = class DictionaryGenerator {
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');
2020-08-28 04:39:03 +02:00
// Load dictionary
fs.readFile(options.path, 'utf8', (err, data) => {
if (err) throw err;
2020-08-28 04:39:03 +02:00
this.dictionary = data.split(/[\n\r]+/);
2020-08-28 04:39:03 +02:00
if (readyCallback) readyCallback();
});
}
2020-08-28 04:39:03 +02:00
// Generates a dictionary-based key, of keyLength words
createKey(keyLength){
let text = '';
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];
}
2020-08-28 04:39:03 +02:00
return text;
}
};