Browse Source

add password chars templates

pull/2/head
Guillaume Vincent 9 years ago
parent
commit
b1ce44dc9d
2 changed files with 71 additions and 15 deletions
  1. +23
    -6
      app/crypto.js
  2. +48
    -9
      tests/crypto.tests.js

+ 23
- 6
app/crypto.js View File

@@ -1,11 +1,11 @@
export function string2Uint8Array(text) {
var buf = new ArrayBuffer(text.length * 2); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = text.length; i < strLen; i++) {
bufView[i] = text.charCodeAt(i);
var bytesPerChar = 2;
var buffer = new ArrayBuffer(text.length * bytesPerChar);
var uint8Array = new Uint8Array(buffer);
for (let i = 0; i < text.length; i++) {
uint8Array[i] = text.charCodeAt(i);
}
return bufView;
return uint8Array;
}
export function getTemplate(templates, index) {
@@ -17,3 +17,20 @@ export function encode(template, indexes) {
return encodedArray.join('');
}
export var passwordChars = {
V: "AEIOUY",
C: "BCDFGHJKLMNPQRSTVWXZ",
v: "aeiouy",
c: "bcdfghjklmnpqrstvwxz",
A: "AEIOUYBCDFGHJKLMNPQRSTVWXZ",
a: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz",
n: "0123456789",
s: "@&%?,=[]_:-+*$#!'^~;()/.",
x: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!'^~;()/."
};
export function getPasswordChar(charType, index) {
var passwordChar = passwordChars[charType];
return passwordChar[index % passwordChar.length];
}

+ 48
- 9
tests/crypto.tests.js View File

@@ -9,16 +9,55 @@ describe('crypto', function () {
assert.equal('a', String.fromCharCode(uint8Array[0]));
});

it('should get a template based on modulo of the index', function () {
var templates = ['template1', 'template2', 'template3'];
assert.equal('template2', crypto.getTemplate(templates, 4));
assert.equal('template2', crypto.getTemplate(templates, 10));
describe('getTemplate', function () {
it('should get a template based on modulo of the index', function () {
var templates = ['template1', 'template2', 'template3'];
assert.equal('template2', crypto.getTemplate(templates, 4));
assert.equal('template2', crypto.getTemplate(templates, 10));
});
});

it('should return char inside template based on modulo of the indexes', function () {
var template = '0123456789';
assert.equal('01', crypto.encode(template, [20, 11]));
assert.equal('01', crypto.encode(template, [20, 21]));
assert.equal('29', crypto.encode(template, [12, 19]));
describe('encode', function () {
it('should return char inside template based on modulo of the indexes', function () {
var template = '0123456789';
assert.equal('01', crypto.encode(template, [20, 11]));
assert.equal('01', crypto.encode(template, [20, 21]));
assert.equal('29', crypto.encode(template, [12, 19]));
});
});

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

Loading…
Cancel
Save