Browse Source

add first cryptographic methods

pull/2/head
Guillaume Vincent 9 years ago
parent
commit
405a628d09
6 changed files with 66 additions and 34 deletions
  1. +19
    -0
      app/crypto.js
  2. +7
    -0
      app/lesspass.js
  3. +0
    -14
      app/password-generator.js
  4. +24
    -0
      tests/crypto.tests.js
  5. +16
    -0
      tests/lesspass.tests.js
  6. +0
    -20
      tests/password-generator.tests.js

+ 19
- 0
app/crypto.js View File

@@ -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('');
}

+ 7
- 0
app/lesspass.js View File

@@ -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);
}

+ 0
- 14
app/password-generator.js View File

@@ -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;
}());

+ 24
- 0
tests/crypto.tests.js View File

@@ -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]));
});
});

+ 16
- 0
tests/lesspass.tests.js View File

@@ -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'));
});
});

+ 0
- 20
tests/password-generator.tests.js View File

@@ -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'));
});
});
}());

Loading…
Cancel
Save