mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 09:50:17 +01:00
Rewrote (and fixed) dictionary key generator
This commit is contained in:
parent
2e2b69c43c
commit
1efc62a8c5
1 changed files with 15 additions and 19 deletions
|
@ -1,32 +1,28 @@
|
||||||
|
const winston = require('winston');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
module.exports = class DictionaryGenerator {
|
module.exports = class DictionaryGenerator {
|
||||||
|
|
||||||
constructor(options, readyCallback){
|
constructor({ path } = {}, readyCallback){
|
||||||
// Check options format
|
//check for dictionary path
|
||||||
if (!options) throw Error('No options passed to generator');
|
if (!path){
|
||||||
if (!options.path) throw Error('No dictionary path specified in options');
|
winston.error('No dictionary path specified in options');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Load dictionary
|
//load dictionary
|
||||||
fs.readFile(options.path, 'utf8', (err, data) => {
|
if (!fs.existsSync(path)){
|
||||||
if (err) throw err;
|
winston.error(`Dictionary file "${path}" doesn't exist`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
this.dictionary = data.split(/[\n\r]+/);
|
this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm);
|
||||||
|
if (readyCallback) readyCallback();
|
||||||
if (readyCallback) readyCallback();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a dictionary-based key, of keyLength words
|
// Generates a dictionary-based key, of keyLength words
|
||||||
createKey(keyLength){
|
createKey(keyLength){
|
||||||
let text = '';
|
return Array(keyLength).fill().map(() => this.dictionary[ Math.floor(Math.random() * this.dictionary.length) ] ).join('');
|
||||||
|
|
||||||
for (let i = 0; i < keyLength; i++){
|
|
||||||
const index = Math.floor(Math.random() * this.dictionary.length);
|
|
||||||
text += this.dictionary[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue