@@ -1,6 +1,5 @@ | |||||
export function string2Uint8Array(text) { | export function string2Uint8Array(text) { | ||||
var bytesPerChar = 2; | |||||
var buffer = new ArrayBuffer(text.length * bytesPerChar); | |||||
var buffer = new ArrayBuffer(text.length); | |||||
var uint8Array = new Uint8Array(buffer); | var uint8Array = new Uint8Array(buffer); | ||||
for (let i = 0; i < text.length; i++) { | for (let i = 0; i < text.length; i++) { | ||||
uint8Array[i] = text.charCodeAt(i); | uint8Array[i] = text.charCodeAt(i); | ||||
@@ -8,13 +7,39 @@ export function string2Uint8Array(text) { | |||||
return uint8Array; | return uint8Array; | ||||
} | } | ||||
export function getTemplate(templates, index) { | |||||
return templates[index % templates.length]; | |||||
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(template, indexes) { | |||||
var encodedArray = indexes.map(index => template[index % template.length]); | |||||
return encodedArray.join(''); | |||||
export function encode(hash, passwordType) { | |||||
var template = getTemplate(passwordType); | |||||
return string2Uint8Array(hash).map( | |||||
(charCode, index) => { | |||||
var charType = getCharType(template, index); | |||||
return getPasswordChar(charType, charCode); | |||||
} | |||||
); | |||||
} | |||||
function elements_with_index(elements) { | |||||
return elements.map((element, index) => index + ':' + element); | |||||
} | } | ||||
export var passwordChars = { | export var passwordChars = { | ||||
@@ -34,3 +59,6 @@ export function getPasswordChar(charType, index) { | |||||
return passwordChar[index % passwordChar.length]; | return passwordChar[index % passwordChar.length]; | ||||
} | } | ||||
export function getCharType(template, index) { | |||||
return template[index % template.length]; | |||||
} |
@@ -9,20 +9,32 @@ describe('crypto', function () { | |||||
assert.equal('a', String.fromCharCode(uint8Array[0])); | assert.equal('a', String.fromCharCode(uint8Array[0])); | ||||
}); | }); | ||||
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)); | |||||
describe('templates', function () { | |||||
it('should get a template from user password type entries', function () { | |||||
assert.equal('cv', crypto.getTemplate('l')); | |||||
assert.equal('CV', crypto.getTemplate('u')); | |||||
assert.equal('cvn', crypto.getTemplate('ln')); | |||||
assert.equal('ns', crypto.getTemplate('ns')); | |||||
}); | |||||
it('should return char inside template based on modulo of the index', function () { | |||||
var template = 'cv'; | |||||
assert.equal('c', crypto.getCharType(template, 0)); | |||||
assert.equal('v', crypto.getCharType(template, 1)); | |||||
assert.equal('c', crypto.getCharType(template, 10)); | |||||
}); | }); | ||||
}); | }); | ||||
describe('encode', function () { | 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])); | |||||
it('should return password size same size of hash given', function () { | |||||
var hash = 'Y2Vi2a112A'; | |||||
var passwordType = 'lun'; | |||||
assert.equal(10, crypto.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(crypto.encode(hash, passwordType1), crypto.encode(hash, passwordType2)); | |||||
}); | }); | ||||
}); | }); | ||||