From 076d74f7d8da49695d71b77887e38459c76e06e9 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Fri, 18 Nov 2016 12:01:09 +0100 Subject: [PATCH] add getSetOfCharacters v2 method --- index.js | 3 ++- src/v2.js | 21 ++++++++++++++++ tests/v2/setOfCharacters.tests.js | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/v2/setOfCharacters.tests.js diff --git a/index.js b/index.js index 82d1537..0065932 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,8 @@ module.exports = { _getCharType: _getCharType, _getPasswordChar: _getPasswordChar, _createHmac: _createHmac, - _calcEntropy: v2.calcEntropy + _calcEntropy: v2.calcEntropy, + _getSetOfCharacters: v2.getSetOfCharacters, }; function _encryptLogin(login, masterPassword, options) { diff --git a/src/v2.js b/src/v2.js index 13c59cc..ca099d7 100644 --- a/src/v2.js +++ b/src/v2.js @@ -15,4 +15,25 @@ exports.calcEntropy = function (site, login, masterPassword, passwordProfile) { } }); }) +}; + +exports.getSetOfCharacters = function (passwordProfile) { + var subsetOfCharacters = { + lowercase: 'abcdefghijklmnopqrstuvwxyz', + uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', + digits: '0123456789', + symbols: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' + }; + + if (typeof passwordProfile === 'undefined') { + return subsetOfCharacters.lowercase + subsetOfCharacters.uppercase + subsetOfCharacters.digits; + } + + var setOfCharacters = ''; + ['lowercase', 'uppercase', 'digits', 'symbols'].forEach(function (subset) { + if (passwordProfile.hasOwnProperty(subset) && passwordProfile[subset]) { + setOfCharacters += subsetOfCharacters[subset] + } + }); + return setOfCharacters; }; \ No newline at end of file diff --git a/tests/v2/setOfCharacters.tests.js b/tests/v2/setOfCharacters.tests.js new file mode 100644 index 0000000..cb4b933 --- /dev/null +++ b/tests/v2/setOfCharacters.tests.js @@ -0,0 +1,52 @@ +var assert = chai.assert; + +describe('LessPass', function () { + describe('set of characters', function () { + it('get default set of characters', function () { + var setOfCharacters = LessPass._getSetOfCharacters(); + assert.equal('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', setOfCharacters); + assert.equal(26 * 2 + 10, setOfCharacters.length); + }); + it('get default set of characters with object', function () { + var setOfCharacters = LessPass._getSetOfCharacters({ + lowercase: true, + uppercase: true, + digits: true, + symbols: false + }); + assert.equal('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', setOfCharacters); + assert.equal(26 * 2 + 10, setOfCharacters.length); + }); + it('get set of characters only lowercase', function () { + var setOfCharacters = LessPass._getSetOfCharacters({lowercase: true}); + assert.equal('abcdefghijklmnopqrstuvwxyz', setOfCharacters); + assert.equal(26, setOfCharacters.length); + }); + it('get set of characters only uppercase', function () { + var setOfCharacters = LessPass._getSetOfCharacters({uppercase: true}); + assert.equal('ABCDEFGHIJKLMNOPQRSTUVWXYZ', setOfCharacters); + assert.equal(26, setOfCharacters.length); + }); + it('get set of characters only digits', function () { + var setOfCharacters = LessPass._getSetOfCharacters({digits: true}); + assert.equal('0123456789', setOfCharacters); + assert.equal(10, setOfCharacters.length); + }); + it('get set of characters only symbols', function () { + var setOfCharacters = LessPass._getSetOfCharacters({symbols: true}); + assert.equal('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', setOfCharacters); + assert.equal(32, setOfCharacters.length); + }); + it('get set of characters concat two subsets', function () { + var setOfCharacters = LessPass._getSetOfCharacters({lowercase: true, uppercase: true}); + assert.equal('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', setOfCharacters); + assert.equal(26 * 2, setOfCharacters.length); + }); + it('get set of characters doesn\'t care of key order in options', function () { + assert.equal( + LessPass._getSetOfCharacters({digits: true, symbols: true}), + LessPass._getSetOfCharacters({symbols: true, digits: true}) + ); + }); + }); +}); \ No newline at end of file