mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-01 01:30:21 +01:00
35 lines
1,011 B
JavaScript
35 lines
1,011 B
JavaScript
var rand = require('random-js');
|
|
var fs = require('fs')
|
|
var dictionary;
|
|
var randomEngine = rand.engines.nativeMath;
|
|
var random;
|
|
|
|
var DictionaryGenerator = function(options) {
|
|
//Options
|
|
if (!options)
|
|
return done(Error('No options passed to generator'));
|
|
if(!options.path)
|
|
return done(Error('No dictionary path specified in options'));
|
|
|
|
//Load dictionary
|
|
fs.readFile(options.path, 'utf8', (err,data) => {
|
|
if(err) throw err;
|
|
this.dictionary = data.split(',');
|
|
|
|
//Remove any non alpha-numeric characters
|
|
for(var i = 0; i < this.dictionary.length; i++)
|
|
this.dictionary[i] = this.dictionary[i].replace(/\W/g,'');
|
|
|
|
this.random = rand.integer(0, this.dictionary.length);
|
|
});
|
|
};
|
|
|
|
//Generates a dictionary-based key, of keyLength words
|
|
DictionaryGenerator.prototype.createKey = function(keyLength) {
|
|
var text = '';
|
|
for(var i = 0; i < keyLength; i++)
|
|
text += this.dictionary[random(randomEngine)];
|
|
return text;
|
|
};
|
|
|
|
module.exports = DictionaryGenerator;
|