Browse Source

move methods into an object

pull/2/head
Guillaume Vincent 9 years ago
parent
commit
1b5fd189a0
2 changed files with 129 additions and 142 deletions
  1. +67
    -66
      app/lesspass.js
  2. +62
    -76
      tests/lesspass.tests.js

+ 67
- 66
app/lesspass.js View File

@@ -1,78 +1,79 @@
import crypto from 'crypto';
export function create_hash(key, text, length = 10, counter = 1) {
var password = crypto.createHmac('sha256', key).update(text + counter.toString()).digest('hex');
return password.substring(0, length);
}
export function create_password(master_password, site, password_info) {
var hash = create_hash(master_password,site,password_info.size);
return encode(hash, password_info.type);
}
export function string2Uint8Array(text) {
var buffer = new ArrayBuffer(text.length);
var uint8Array = new Uint8Array(buffer);
for (let i = 0; i < text.length; i++) {
uint8Array[i] = text.charCodeAt(i);
export class lesspass {
static create_password(master_password, site_information) {
var hash = this._create_hash(master_password, site_information);
return this._encode(hash, site_information.password_type);
}
return uint8Array;
}
export function getTemplate(passwordType) {
var templates = {
l: "cv",
u: "CV",
n: "n",
s: "s",
lu: "cvCV",
ln: "cvn",
ls: "cvs",
un: "CVn",
us: "CVs",
ns: "ns",
lun: "cvCVn",
uns: "CVns",
lns: "cvns",
lus: "cvCVs",
luns: "cvCVns"
};
return templates[passwordType];
}
export function encode(hash, passwordType) {
var template = getTemplate(passwordType);
var password = '';
string2Uint8Array(hash).map(
(charCode, index) => {
var charType = getCharType(template, index);
password += getPasswordChar(charType, charCode);
static _create_hash(master_password, {site_name, password_length=10, counter= 1}) {
var salt = site_name + counter.toString();
var password = crypto.createHmac('sha256', master_password).update(salt).digest('hex');
return password.substring(0, password_length);
}
static _string2charCodes(text) {
var buffer = new ArrayBuffer(text.length);
var charCodes = new Uint8Array(buffer);
for (let i = 0; i < text.length; i++) {
charCodes[i] = text.charCodeAt(i);
}
);
return password;
}
return charCodes;
}
function elements_with_index(elements) {
return elements.map((element, index) => index + ':' + element);
}
static _getTemplate(passwordType) {
var templates = {
l: "cv",
u: "CV",
n: "n",
s: "s",
lu: "a",
ln: "cvn",
ls: "cvs",
un: "CVn",
us: "CVs",
ns: "ns",
lun: "an",
uns: "CVns",
lns: "cvns",//alphanumeric lowercase
lus: "as",
luns: "cvCVns"//all, strong
};
return templates[passwordType];
}
export var passwordChars = {
V: "AEIOUY",
C: "BCDFGHJKLMNPQRSTVWXZ",
v: "aeiouy",
c: "bcdfghjklmnpqrstvwxz",
A: "AEIOUYBCDFGHJKLMNPQRSTVWXZ",
a: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz",
n: "0123456789",
s: "@&%?,=[]_:-+*$#!'^~;()/.",
x: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!'^~;()/."
};
static _getCharType(template, index) {
return template[index % template.length];
}
static _getPasswordChar(charType, index) {
var passwordsChars = {
V: "AEIOUY",
C: "BCDFGHJKLMNPQRSTVWXZ",
v: "aeiouy",
c: "bcdfghjklmnpqrstvwxz",
A: "AEIOUYBCDFGHJKLMNPQRSTVWXZ",
a: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz",
n: "0123456789",
s: "@&%?,=[]_:-+*$#!'^~;()/.",
x: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!'^~;()/."
};
var passwordChar = passwordsChars[charType];
return passwordChar[index % passwordChar.length];
}
export function getPasswordChar(charType, index) {
var passwordChar = passwordChars[charType];
return passwordChar[index % passwordChar.length];
}
export function getCharType(template, index) {
return template[index % template.length];
static _encode(hash, passwordType) {
var template = this._getTemplate(passwordType);
var password = '';
this._string2charCodes(hash).map(
(charCode, index) => {
let charType = this._getCharType(template, index);
password += this._getPasswordChar(charType, charCode);
}
);
return password;
}
}

+ 62
- 76
tests/lesspass.tests.js View File

@@ -1,102 +1,88 @@
import assert from 'assert';
import * as lesspass from '../app/lesspass';
import {lesspass} from '../app/lesspass';

describe('lesspass', function () {
it('should return a default password with a size of 10', function () {
var generatedPassword = lesspass.create_hash('password', 'facebook');
assert.equal(10, generatedPassword.length);
});
it('should allow to change password length', function () {
var generatedPassword = lesspass.create_hash('password', 'facebook', 12);
assert.equal(12, generatedPassword.length);
});
it('should allow to change password with increment', function () {
var password1 = lesspass.create_hash('password', 'facebook', 12);
var password2 = lesspass.create_hash('password', 'facebook', 12, 2);
assert.notEqual(password1, password2)
});
it('should create password', function () {
var master_password = 'password';
var password_info = {
'size': '12',
'type': 'luns',
'counter': '1'
};
var site = 'facebook';
assert.equal('veXU8[syCE4&', lesspass.create_password(master_password, site, password_info));
describe('lesspass', ()=> {
describe('public api', ()=> {
it('should create password', function () {
var master_password = "password";
var site_information = {
'site_name': 'facebook',
'password_length': 12,
'password_type': 'luns',
'counter': 1
};
assert.equal('veXU8[syCE4&', lesspass.create_password(master_password, site_information));
});
});
});


describe('crypto', function () {
it('should convert string into array of char code', function () {
var uint8Array = lesspass.string2Uint8Array('ab');
assert.equal(97, uint8Array[0]);
assert.equal(98, uint8Array[1]);
assert.equal('a', String.fromCharCode(uint8Array[0]));
describe('hash', ()=> {
it('should have default length of 10', ()=> {
var master_password = "password";
var site_information = {'site_name': 'facebook'};
assert.equal(10, lesspass._create_hash(master_password, site_information).length);
});
it('should be able to create hash with defined length', ()=> {
var master_password = "password";
var site_information = {
'site_name': 'facebook',
'password_length': 12
};
assert.equal(12, lesspass._create_hash(master_password, site_information).length);
});
it('should return two different password if counter different', ()=> {
var master_password = "password";
var old_site_information = {'site_name': 'facebook'};
var site_information = {'site_name': 'facebook', 'counter': 2};
assert.notEqual(
lesspass._create_hash(master_password, site_information),
lesspass._create_hash(master_password, old_site_information)
);
});
});

describe('templates', function () {
it('should get a template from user password type entries', function () {
assert.equal('cv', lesspass.getTemplate('l'));
assert.equal('CV', lesspass.getTemplate('u'));
assert.equal('cvn', lesspass.getTemplate('ln'));
assert.equal('ns', lesspass.getTemplate('ns'));
describe('password templates', ()=> {
it('should get template from password type', ()=> {
assert.equal('cv', lesspass._getTemplate('l'));
assert.equal('CV', lesspass._getTemplate('u'));
assert.equal('cvn', lesspass._getTemplate('ln'));
assert.equal('ns', lesspass._getTemplate('ns'));
});
xit('should get template from alias', ()=> {
assert.equal(lesspass._getTemplate('luns'), lesspass._getTemplate('strong'));
assert.equal(lesspass._getTemplate('an'), lesspass._getTemplate('alphanumeric'));
assert.equal(lesspass._getTemplate('n'), lesspass._getTemplate('numeric'));
});
it('should return char inside template based on modulo of the index', function () {
var template = 'cv';
assert.equal('c', lesspass.getCharType(template, 0));
assert.equal('v', lesspass.getCharType(template, 1));
assert.equal('c', lesspass.getCharType(template, 10));
assert.equal('c', lesspass._getCharType(template, 0));
assert.equal('v', lesspass._getCharType(template, 1));
assert.equal('c', lesspass._getCharType(template, 10));
});
});

describe('encode', function () {
describe('crypto', ()=> {
it('should convert a string into a char code table', ()=> {
var charCodes = lesspass._string2charCodes('ab40f6ee71');
assert.equal(97, charCodes[0]);
assert.equal(98, charCodes[1]);
assert.equal(10, charCodes.length);
});
it('should return password size same size of hash given', function () {
var hash = 'Y2Vi2a112A';
var passwordType = 'lun';
assert.equal(10, lesspass.encode(hash, passwordType).length);
assert.equal(10, lesspass._encode(hash, passwordType).length);
});
it('for the same hash should return value depending on the passwordType', function () {
var hash = 'a';
var passwordType1 = 'n';
var passwordType2 = 'l';
assert.notEqual(lesspass.encode(hash, passwordType1), lesspass.encode(hash, passwordType2));
});
});

describe('getPasswordChar', function () {
it('should have vowel type', function () {
assert.equal(6, lesspass.passwordChars['v'].length);
assert.equal(6, lesspass.passwordChars['V'].length);
});
it('should have consonant type', function () {
assert.equal(20, lesspass.passwordChars['c'].length);
assert.equal(20, lesspass.passwordChars['C'].length);
});
it('should have alphabet uppercase type', function () {
assert.equal(26, lesspass.passwordChars['A'].length);
});
it('should have alphabet uppercase and lowercase type', function () {
assert.equal(52, lesspass.passwordChars['a'].length);
});
it('should have number type', function () {
assert.equal(10, lesspass.passwordChars['n'].length);
});
it('should have symbol type', function () {
assert.ok(lesspass.passwordChars['s'].length);
});
it('should have all type', function () {
assert.equal(86, lesspass.passwordChars['x'].length);
assert.notEqual(lesspass._encode(hash, passwordType1), lesspass._encode(hash, passwordType2));
});
it('should return char based on its type and on his index', function () {
var typeVowel = 'V';
assert.equal('A', lesspass.getPasswordChar(typeVowel, 0));
assert.equal('A', lesspass._getPasswordChar(typeVowel, 0));
});
it('should return char and modulo if overflow', function () {
var typeVowel = 'V';
assert.equal('E', lesspass.getPasswordChar(typeVowel, 1));
assert.equal('E', lesspass.getPasswordChar(typeVowel, 7));
assert.equal('E', lesspass._getPasswordChar(typeVowel, 1));
assert.equal('E', lesspass._getPasswordChar(typeVowel, 7));
});
});
});

Loading…
Cancel
Save