Commit e54a8601 authored by Klas af Geijerstam's avatar Klas af Geijerstam Committed by GitHub
Browse files

Added dictionary.js

A key generator that uses a dictionary to create its keys
parent 5a8697cd
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
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;