Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
build/
npm-debug.log
.*.sw?
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
21 changes: 17 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
"name": "skip32",
"description": "skip32 (Skipjack cipher for 32-bit numbers) for node",
"version": "1.2.0",
"contributors": [
{ "name": "Ken Woodruff", "email": "ken.woodruff@gmail.com" },
{ "name": "Imuli", "email": "i@imu.li" }
"contributors": [
{
"name": "Ken Woodruff",
"email": "ken.woodruff@gmail.com"
},
{
"name": "Imuli",
"email": "i@imu.li"
}
],
"repository": "http://github.com/femto113/node-skip32.git",
"main": "skip32.js"
"main": "skip32.js",
"devDependencies": {
"expect.js": "^0.3.1",
"mocha": "^2.2.1"
},
"scripts": {
"test" :"mocha"
}
}
124 changes: 74 additions & 50 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,74 @@
var Skip32 = require('./skip32.js').Skip32;

// these are the default test values from the original C code
var KEY = [ 0x00,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11 ];
var INPUT = parseInt("33221100", 16)
var ENCRYPTED = parseInt("819d5f1f", 16);

console.log("running the default test case");

var s = new Skip32(KEY);
var e = s.encrypt(INPUT);
var d = s.decrypt(e);

console.log("expected:", INPUT.toString(16), "->", ENCRYPTED.toString(16), "->", INPUT.toString(16));
console.log(" got:", INPUT.toString(16), "->", e.toString(16), "->", d.toString(16));
console.log();

// some speed tests follow

var N = 50000, start;

console.log("encrypting/decrypting", N, "numbers using Skip32...");
start = new Date();
for (var i = 0; i < N; i++) {
if (s.decrypt(s.encrypt(i)) != i) {
console.log("Skip32 decrypt/encrypt cycle failed for ", i);
break;
}
}
var elapsed = (new Date()) - start;
console.log("...finished in", elapsed / 1000.0, "seconds", "(approx.", Math.round(N / (elapsed / 1000.0)), "cycles per second)");

// for comparison do the same thing using one of the fastest built in ciphers (~7x slower in my tests)
// NOTE: the crypto library is not designed well for this particular usage, e.g. the cipher object isn't reusable

var crypto = require('crypto');
var cipher_name = 'rc4'
console.log("encrypting/decrypting", N, "numbers using crypto &", cipher_name, "...");
start = new Date();
for (var i = 0; i < N; i++) {
var cipher = crypto.createCipher(cipher_name, 'secret'), decipher = crypto.createDecipher(cipher_name, 'secret');
e = cipher.update(i.toString(16), 'utf8', 'hex') + cipher.final('hex');
d = parseInt(decipher.update(e, 'hex', 'utf8') + decipher.final('utf8'), 16);
if (i != d) {
console.log(cipher_name, "decrypt/encrypt cycle failed for ", i);
break;
}
}
var elapsed = (new Date()) - start;
console.log("...finished in", elapsed / 1000.0, "seconds", "(approx.", Math.round(N / (elapsed / 1000.0)), "cycles per second)");
'use strict';
// jshint node: true
/* globals describe, it */
var expect = require('expect.js');
var Skip32 = require('./skip32').Skip32;

describe('Skip32', function() {
describe('encrypt/decrypt', function() {
var key = [0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, 0xff, 0xff];
var skip32 = new Skip32(key);

it('handles 2^0', function() {
var n = Math.pow(2, 0);
expect(skip32.decrypt(skip32.encrypt(n))).to.be(n);
});

it('handles 2^1', function() {
var n = Math.pow(2, 1);
expect(skip32.decrypt(skip32.encrypt(n))).to.be(n);
});

it('handles 2^10', function() {
var n = Math.pow(2, 10);
expect(skip32.decrypt(skip32.encrypt(n))).to.be(n);
});

it('handles 2^30', function() {
var n = Math.pow(2, 30);
expect(skip32.decrypt(skip32.encrypt(n))).to.be(n);
});

it('handles 2^31', function() {
var n = Math.pow(2, 31);
expect(skip32.decrypt(skip32.encrypt(n))).to.be(n);
});

it('handles 2^32-1', function() {
var n = Math.pow(2, 32) - 1;
expect(skip32.decrypt(skip32.encrypt(n))).to.be(n);
});
});

describe('encrypt', function() {
it('handles vector from original C version', function() {
var key = [0x00, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
var input = 0x33221100;
var expected = 0x819d5f1f;

var skip32 = new Skip32(key);
var actual = skip32.encrypt(input);
expect(actual).to.be(expected);
});

it('it consistent with C version (1)', function() {
var key = [0x00, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
var input = 0xffffffff;
var expected = 0x32ff23b2;

var skip32 = new Skip32(key);
var actual = skip32.encrypt(input);
expect(actual).to.be(expected);
});

it('it consistent with C version (2)', function() {
var key = [0xff, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
var input = 0xffffff44;
var expected = 0x5fbc2a31;

var skip32 = new Skip32(key);
var actual = skip32.encrypt(input);
expect(actual).to.be(expected);
});
});
});