commit
ee03e7cd78
7 changed files with 145 additions and 63 deletions
@ -1,24 +1,32 @@ |
|||||||
var fs = require('fs'); |
const fs = require('fs'); |
||||||
|
|
||||||
var DictionaryGenerator = function(options) { |
|
||||||
//Options
|
|
||||||
if (!options) throw Error('No options passed to generator'); |
|
||||||
if (!options.path) throw Error('No dictionary path specified in options'); |
|
||||||
|
|
||||||
//Load dictionary
|
|
||||||
fs.readFile(options.path, 'utf8', (err, data) => { |
|
||||||
if (err) throw err; |
|
||||||
this.dictionary = data.split(/[\n\r]+/); |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
//Generates a dictionary-based key, of keyLength words
|
module.exports = class DictionaryGenerator { |
||||||
DictionaryGenerator.prototype.createKey = function(keyLength) { |
|
||||||
var text = ''; |
|
||||||
for(var i = 0; i < keyLength; i++) |
|
||||||
text += this.dictionary[Math.floor(Math.random() * this.dictionary.length)]; |
|
||||||
|
|
||||||
return text; |
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'); |
||||||
|
|
||||||
|
// Load dictionary
|
||||||
|
fs.readFile(options.path, 'utf8', (err, data) => { |
||||||
|
if (err) throw err; |
||||||
|
|
||||||
|
this.dictionary = data.split(/[\n\r]+/); |
||||||
|
|
||||||
|
if (readyCallback) readyCallback(); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
module.exports = DictionaryGenerator; |
// Generates a dictionary-based key, of keyLength words
|
||||||
|
createKey(keyLength) { |
||||||
|
let text = ''; |
||||||
|
|
||||||
|
for (let i = 0; i < keyLength; i++) { |
||||||
|
const index = Math.floor(Math.random() * this.dictionary.length); |
||||||
|
text += this.dictionary[index]; |
||||||
|
} |
||||||
|
|
||||||
|
return text; |
||||||
|
} |
||||||
|
|
||||||
|
}; |
||||||
|
@ -1,33 +1,27 @@ |
|||||||
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
// Draws inspiration from pwgen and http://tools.arantius.com/password
|
||||||
var PhoneticKeyGenerator = function() { |
|
||||||
// No options
|
|
||||||
}; |
|
||||||
|
|
||||||
// Generate a phonetic key
|
const randOf = (collection) => { |
||||||
PhoneticKeyGenerator.prototype.createKey = function(keyLength) { |
return () => { |
||||||
var text = ''; |
return collection[Math.floor(Math.random() * collection.length)]; |
||||||
var start = Math.round(Math.random()); |
}; |
||||||
for (var i = 0; i < keyLength; i++) { |
|
||||||
text += (i % 2 == start) ? this.randConsonant() : this.randVowel(); |
|
||||||
} |
|
||||||
return text; |
|
||||||
}; |
}; |
||||||
|
|
||||||
PhoneticKeyGenerator.consonants = 'bcdfghjklmnpqrstvwxyz'; |
// Helper methods to get an random vowel or consonant
|
||||||
PhoneticKeyGenerator.vowels = 'aeiou'; |
const randVowel = randOf('aeiou'); |
||||||
|
const randConsonant = randOf('bcdfghjklmnpqrstvwxyz'); |
||||||
|
|
||||||
// Get an random vowel
|
module.exports = class PhoneticKeyGenerator { |
||||||
PhoneticKeyGenerator.prototype.randVowel = function() { |
|
||||||
return PhoneticKeyGenerator.vowels[ |
|
||||||
Math.floor(Math.random() * PhoneticKeyGenerator.vowels.length) |
|
||||||
]; |
|
||||||
}; |
|
||||||
|
|
||||||
// Get an random consonant
|
// Generate a phonetic key of alternating consonant & vowel
|
||||||
PhoneticKeyGenerator.prototype.randConsonant = function() { |
createKey(keyLength) { |
||||||
return PhoneticKeyGenerator.consonants[ |
let text = ''; |
||||||
Math.floor(Math.random() * PhoneticKeyGenerator.consonants.length) |
const start = Math.round(Math.random()); |
||||||
]; |
|
||||||
}; |
|
||||||
|
|
||||||
module.exports = PhoneticKeyGenerator; |
for (let i = 0; i < keyLength; i++) { |
||||||
|
text += (i % 2 == start) ? randConsonant() : randVowel(); |
||||||
|
} |
||||||
|
|
||||||
|
return text; |
||||||
|
} |
||||||
|
|
||||||
|
}; |
||||||
|
@ -1,19 +1,20 @@ |
|||||||
var RandomKeyGenerator = function(options) { |
module.exports = class RandomKeyGenerator { |
||||||
if (!options) { |
|
||||||
options = {}; |
// Initialize a new generator with the given keySpace
|
||||||
|
constructor(options = {}) { |
||||||
|
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
||||||
} |
} |
||||||
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
||||||
}; |
|
||||||
|
|
||||||
// Generate a random key
|
// Generate a key of the given length
|
||||||
RandomKeyGenerator.prototype.createKey = function(keyLength) { |
createKey(keyLength) { |
||||||
var text = ''; |
var text = ''; |
||||||
var index; |
|
||||||
for (var i = 0; i < keyLength; i++) { |
for (var i = 0; i < keyLength; i++) { |
||||||
index = Math.floor(Math.random() * this.keyspace.length); |
const index = Math.floor(Math.random() * this.keyspace.length); |
||||||
text += this.keyspace.charAt(index); |
text += this.keyspace.charAt(index); |
||||||
|
} |
||||||
|
|
||||||
|
return text; |
||||||
} |
} |
||||||
return text; |
|
||||||
}; |
|
||||||
|
|
||||||
module.exports = RandomKeyGenerator; |
}; |
||||||
|
@ -0,0 +1,33 @@ |
|||||||
|
/* global describe, it */ |
||||||
|
|
||||||
|
const assert = require('assert'); |
||||||
|
|
||||||
|
const fs = require('fs'); |
||||||
|
|
||||||
|
const Generator = require('../../lib/key_generators/dictionary'); |
||||||
|
|
||||||
|
describe('RandomKeyGenerator', function() { |
||||||
|
describe('randomKey', function() { |
||||||
|
it('should throw an error if given no options', () => { |
||||||
|
assert.throws(() => { |
||||||
|
new Generator(); |
||||||
|
}, Error); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should throw an error if given no path', () => { |
||||||
|
assert.throws(() => { |
||||||
|
new Generator({}); |
||||||
|
}, Error); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should return a key of the proper number of words from the given dictionary', () => { |
||||||
|
const path = '/tmp/haste-server-test-dictionary'; |
||||||
|
const words = ['cat']; |
||||||
|
fs.writeFileSync(path, words.join('\n')); |
||||||
|
|
||||||
|
const gen = new Generator({path}, () => { |
||||||
|
assert.equal('catcatcat', gen.createKey(3)); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,27 @@ |
|||||||
|
/* global describe, it */ |
||||||
|
|
||||||
|
const assert = require('assert'); |
||||||
|
|
||||||
|
const Generator = require('../../lib/key_generators/phonetic'); |
||||||
|
|
||||||
|
const vowels = 'aeiou'; |
||||||
|
const consonants = 'bcdfghjklmnpqrstvwxyz'; |
||||||
|
|
||||||
|
describe('RandomKeyGenerator', () => { |
||||||
|
describe('randomKey', () => { |
||||||
|
it('should return a key of the proper length', () => { |
||||||
|
const gen = new Generator(); |
||||||
|
assert.equal(6, gen.createKey(6).length); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should alternate consonants and vowels', () => { |
||||||
|
const gen = new Generator(); |
||||||
|
|
||||||
|
const key = gen.createKey(3); |
||||||
|
|
||||||
|
assert.ok(consonants.includes(key[0])); |
||||||
|
assert.ok(consonants.includes(key[2])); |
||||||
|
assert.ok(vowels.includes(key[1])); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,19 @@ |
|||||||
|
/* global describe, it */ |
||||||
|
|
||||||
|
const assert = require('assert'); |
||||||
|
|
||||||
|
const Generator = require('../../lib/key_generators/random'); |
||||||
|
|
||||||
|
describe('RandomKeyGenerator', () => { |
||||||
|
describe('randomKey', () => { |
||||||
|
it('should return a key of the proper length', () => { |
||||||
|
const gen = new Generator(); |
||||||
|
assert.equal(6, gen.createKey(6).length); |
||||||
|
}); |
||||||
|
|
||||||
|
it('should use a key from the given keyset if given', () => { |
||||||
|
const gen = new Generator({keyspace: 'A'}); |
||||||
|
assert.equal('AAAAAA', gen.createKey(6)); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
Loading…
Reference in new issue