mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 17:50:19 +01:00
Added dictionary.js
A key generator that uses a dictionary to create its keys
This commit is contained in:
parent
5a8697cdd8
commit
e54a860172
1 changed files with 37 additions and 0 deletions
37
lib/key_generators/dictionary.js
Normal file
37
lib/key_generators/dictionary.js
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
var rand = require('random-js');
|
||||||
|
var fs = require('fs')
|
||||||
|
var dictionary;
|
||||||
|
var randomEngine;
|
||||||
|
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;
|
||||||
|
dictionary = data.split(',');
|
||||||
|
|
||||||
|
//Remove any non alpha-numeric characters
|
||||||
|
for(var i = 0; i < dictionary.length; i++){
|
||||||
|
dictionary[i] = dictionary[i].replace(/\W/g,'');
|
||||||
|
}
|
||||||
|
|
||||||
|
random = rand.integer(0,dictionary.length);
|
||||||
|
randomEngine = rand.engines.nativeMath;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
//Generates a dictionary-based key, of keyLength words
|
||||||
|
DictionaryGenerator.prototype.createKey = function(keyLength) {
|
||||||
|
var text = '';
|
||||||
|
for(var i = 0; i < keyLength; i++)
|
||||||
|
text += dictionary[random(randomEngine)];
|
||||||
|
return text;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = DictionaryGenerator;
|
Loading…
Reference in a new issue