Kaynağa Gözat

add render Password tests

pull/342/head
Guillaume Vincent 8 yıl önce
ebeveyn
işleme
6965c0dd41
5 değiştirilmiş dosya ile 56 ekleme ve 0 silme
  1. +1
    -0
      index.js
  2. +1
    -0
      package.json
  3. +16
    -0
      src/v2.js
  4. +28
    -0
      tests/v2/renderPassword.tests.js
  5. +10
    -0
      tests/v2/renderPassword.tests.py

+ 1
- 0
index.js Dosyayı Görüntüle

@@ -16,6 +16,7 @@ module.exports = {
_createHmac: _createHmac,
_calcEntropy: v2.calcEntropy,
_getSetOfCharacters: v2.getSetOfCharacters,
_renderPassword: v2.renderPassword,
};

function _encryptLogin(login, masterPassword, options) {


+ 1
- 0
package.json Dosyayı Görüntüle

@@ -23,6 +23,7 @@
"test:browser": "npm run build && karma start tests/karma.config.js"
},
"dependencies": {
"big-integer": "^1.6.17",
"bluebird": "^3.4.6",
"create-hmac": "^1.1.4",
"pbkdf2": "^3.0.9",


+ 16
- 0
src/v2.js Dosyayı Görüntüle

@@ -1,5 +1,6 @@
var Promise = require("bluebird");
var pbkdf2 = require('pbkdf2');
var bigInt = require("big-integer");

exports.calcEntropy = function (site, login, masterPassword, passwordProfile) {
return new Promise(function (resolve, reject) {
@@ -36,4 +37,19 @@ exports.getSetOfCharacters = function (passwordProfile) {
}
});
return setOfCharacters;
};

function consumeEntropy(generatedPassword, quotient, setOfCharacters, maxLength) {
if (generatedPassword.length >= maxLength) {
return generatedPassword
}
var longDivision = quotient.divmod(setOfCharacters.length);
generatedPassword += setOfCharacters[longDivision.remainder];
return consumeEntropy(generatedPassword, longDivision.quotient, setOfCharacters, maxLength)
}

exports.renderPassword = function (entropy, setOfCharacters, passwordProfile) {
var _passwordProfile = passwordProfile !== undefined ? passwordProfile : {};
var length = _passwordProfile.length || 14;
return consumeEntropy('', bigInt(entropy, 16), setOfCharacters, length);
};

+ 28
- 0
tests/v2/renderPassword.tests.js Dosyayı Görüntüle

@@ -0,0 +1,28 @@
var assert = chai.assert;

describe('LessPass', function () {
it('render password use remainder of long division beetween entropy and set of chars length as an index ', function () {
var entropy = 'dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e';
var setOfCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
assert.equal('y', LessPass._renderPassword(entropy, setOfCharacters)[0]);
});
it('render password use quotient as second entropy recursively', function () {
var entropy = 'dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e';
var setOfCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
assert.equal('5', LessPass._renderPassword(entropy, setOfCharacters)[1]);
});
it('render password has default length of 14', function () {
var entropy = 'dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e';
var setOfCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
assert.equal(14, LessPass._renderPassword(entropy, setOfCharacters).length);
});
it('render password can specify length', function () {
var entropy = 'dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e';
var setOfCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var passwordProfile = {
length: 20
};
assert.equal(20, LessPass._renderPassword(entropy, setOfCharacters, passwordProfile).length);
});

});

+ 10
- 0
tests/v2/renderPassword.tests.py Dosyayı Görüntüle

@@ -0,0 +1,10 @@
def renderPassword(mdp, quotient, alphabet):
if len(mdp) > 14:
return mdp
quotient, remainder = divmod(quotient, len(alphabet))
mdp += alphabet[remainder]
return renderPassword(mdp, quotient, alphabet)


alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
print(renderPassword('', int('dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e', 16), alphabet))

Yükleniyor…
İptal
Kaydet