Skip to content

Commit

Permalink
es6. Closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Nov 2, 2015
1 parent 9bc5a85 commit ea42457
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: node_js

node_js:
- 0.10
- 4.0
- 4
- 5

sudo: false

22 changes: 12 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
'use strict';

// Load modules

var Crypto = require('crypto');
var Boom = require('boom');
const Crypto = require('crypto');
const Boom = require('boom');


// Declare internals

var internals = {};
const internals = {};


// Generate a cryptographically strong pseudo-random data

exports.randomString = function (size) {

var buffer = exports.randomBits((size + 1) * 6);
const buffer = exports.randomBits((size + 1) * 6);
if (buffer instanceof Error) {
return buffer;
}

var string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
const string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
return string.slice(0, size);
};

Expand All @@ -31,7 +33,7 @@ exports.randomBits = function (bits) {
return Boom.internal('Invalid random bits count');
}

var bytes = Math.ceil(bits / 8);
const bytes = Math.ceil(bits / 8);
try {
return Crypto.randomBytes(bytes);
}
Expand All @@ -51,14 +53,14 @@ exports.fixedTimeComparison = function (a, b) {
return false;
}

var mismatch = (a.length === b.length ? 0 : 1);
let mismatch = (a.length === b.length ? 0 : 1);
if (mismatch) {
b = a;
}

for (var i = 0, il = a.length; i < il; ++i) {
var ac = a.charCodeAt(i);
var bc = b.charCodeAt(i);
for (let i = 0; i < a.length; ++i) {
const ac = a.charCodeAt(i);
const bc = b.charCodeAt(i);
mismatch |= (ac ^ bc);
}

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cryptiles",
"description": "General purpose crypto utilities",
"version": "2.0.5",
"version": "3.0.0",
"repository": "git://github.com/hapijs/cryptiles",
"main": "lib/index.js",
"keywords": [
Expand All @@ -10,14 +10,14 @@
"utilites"
],
"engines": {
"node": ">=0.10.40"
"node": ">=4.0.0"
},
"dependencies": {
"boom": "2.x.x"
"boom": "3.x.x"
},
"devDependencies": {
"code": "1.x.x",
"lab": "5.x.x"
"code": "2.x.x",
"lab": "7.x.x"
},
"scripts": {
"test": "lab -a code -t 100 -L",
Expand Down
56 changes: 29 additions & 27 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,102 @@
'use strict';

// Load modules

var Code = require('code');
var Cryptiles = require('..');
var Lab = require('lab');
const Code = require('code');
const Cryptiles = require('..');
const Lab = require('lab');


// Declare internals

var internals = {};
const internals = {};


// Test shortcuts

var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;


describe('randomString()', function () {
describe('randomString()', () => {

it('should generate the right length string', function (done) {
it('should generate the right length string', (done) => {

for (var i = 1; i <= 1000; ++i) {
for (let i = 1; i <= 1000; ++i) {
expect(Cryptiles.randomString(i).length).to.equal(i);
}

done();
});

it('returns an error on invalid bits size', function (done) {
it('returns an error on invalid bits size', (done) => {

expect(Cryptiles.randomString(99999999999999999999).message).to.match(/Failed generating random bits/);
done();
});
});

describe('randomBits()', function () {
describe('randomBits()', () => {

it('returns an error on invalid input', function (done) {
it('returns an error on invalid input', (done) => {

expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');
done();
});
});

describe('fixedTimeComparison()', function () {
describe('fixedTimeComparison()', () => {

var a = Cryptiles.randomString(50000);
var b = Cryptiles.randomString(150000);
const a = Cryptiles.randomString(50000);
const b = Cryptiles.randomString(150000);

it('should take the same amount of time comparing different string sizes', function (done) {
it('should take the same amount of time comparing different string sizes', (done) => {

var now = Date.now();
let now = Date.now();
Cryptiles.fixedTimeComparison(b, a);
var t1 = Date.now() - now;
const t1 = Date.now() - now;

now = Date.now();
Cryptiles.fixedTimeComparison(b, b);
var t2 = Date.now() - now;
const t2 = Date.now() - now;

expect(t2 - t1).to.be.within(-20, 20);
done();
});

it('should return true for equal strings', function (done) {
it('should return true for equal strings', (done) => {

expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);
done();
});

it('should return false for different strings (size, a < b)', function (done) {
it('should return false for different strings (size, a < b)', (done) => {

expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);
done();
});

it('should return false for different strings (size, a > b)', function (done) {
it('should return false for different strings (size, a > b)', (done) => {

expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);
done();
});

it('should return false for different strings (size, a = b)', function (done) {
it('should return false for different strings (size, a = b)', (done) => {

expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);
done();
});

it('should return false when not a string', function (done) {
it('should return false when not a string', (done) => {

expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);
done();
});

it('should return false when not a string (left)', function (done) {
it('should return false when not a string (left)', (done) => {

expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);
done();
Expand Down

0 comments on commit ea42457

Please sign in to comment.