diff --git a/app/crypto.js b/app/crypto.js new file mode 100644 index 0000000..101c224 --- /dev/null +++ b/app/crypto.js @@ -0,0 +1,19 @@ +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); + } + + return bufView; +} + +export function getTemplate(templates, index) { + return templates[index % templates.length]; +} + +export function encode(template, indexes) { + var encodedArray = indexes.map(index => template[index % template.length]); + return encodedArray.join(''); +} + diff --git a/app/lesspass.js b/app/lesspass.js new file mode 100644 index 0000000..6c717ba --- /dev/null +++ b/app/lesspass.js @@ -0,0 +1,7 @@ +import crypto from 'crypto'; + +export function make_password(key, text, length = 10) { + var password = crypto.createHmac('sha256', key).update(text).digest('hex'); + return new Buffer(password).toString('base64').substring(0, length); +} + diff --git a/app/password-generator.js b/app/password-generator.js deleted file mode 100644 index 6cc6535..0000000 --- a/app/password-generator.js +++ /dev/null @@ -1,14 +0,0 @@ -(function () { - 'use strict'; - - var crypto = require('crypto'); - - class PasswordGenerator { - static make_password(key, text, length = 10) { - var password = crypto.createHmac('sha1', key).update(text).digest('hex'); - return new Buffer(password).toString('base64').substring(0, length); - } - } - - module.exports = PasswordGenerator; -}()); diff --git a/tests/crypto.tests.js b/tests/crypto.tests.js new file mode 100644 index 0000000..76c1293 --- /dev/null +++ b/tests/crypto.tests.js @@ -0,0 +1,24 @@ +import assert from 'assert'; +import * as crypto from '../app/crypto'; + +describe('crypto', function () { + it('should convert string into array of char code', function () { + var uint8Array = crypto.string2Uint8Array('ab'); + assert.equal(97, uint8Array[0]); + assert.equal(98, uint8Array[1]); + 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)); + }); + + 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])); + }); +}); \ No newline at end of file diff --git a/tests/lesspass.tests.js b/tests/lesspass.tests.js new file mode 100644 index 0000000..291de7e --- /dev/null +++ b/tests/lesspass.tests.js @@ -0,0 +1,16 @@ +import assert from 'assert'; +import * as lesspass from '../app/lesspass'; + +describe('lesspass', function () { + it('should return a default password with a size of 10', function () { + var generatedPassword = lesspass.make_password('password', 'facebook'); + assert.equal(10, generatedPassword.length); + }); + it('should allow to change password length', function () { + var generatedPassword = lesspass.make_password('password', 'facebook', 12); + assert.equal(12, generatedPassword.length); + }); + it('should hmac with hash sha256', function () { + assert.equal('Y2ViYmVlZm', lesspass.make_password('password', 'facebook')); + }); +}); \ No newline at end of file diff --git a/tests/password-generator.tests.js b/tests/password-generator.tests.js deleted file mode 100644 index 908ed2f..0000000 --- a/tests/password-generator.tests.js +++ /dev/null @@ -1,20 +0,0 @@ -(function () { - 'use strict'; - - var assert = require('assert'); - var PasswordGenerator = require('../app/password-generator'); - - describe('password-generator', function () { - it('should return a default password with a size of 10', function () { - var generatedPassword = PasswordGenerator.make_password('password', 'facebook'); - assert.equal(10, generatedPassword.length); - }); - it('should allow to change password length', function () { - var generatedPassword = PasswordGenerator.make_password('password', 'facebook', 12); - assert.equal(12, generatedPassword.length); - }); - it('should hmac text with master password', function () { - assert.equal('MjE5Y2I1OG', PasswordGenerator.make_password('password', 'facebook')); - }); - }); -}());