From 79a497319de463d7e85b996b4dac16baa065122a Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 26 Mar 2015 10:53:07 +0100 Subject: [PATCH 1/2] add .gitignore for node --- .gitignore | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 751d8b9..123ae94 100644 --- a/.gitignore +++ b/.gitignore @@ -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 From cbdc36d1c7caf35fe52b73a945edc53d144c218e Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 26 Mar 2015 10:53:44 +0100 Subject: [PATCH 2/2] add tests --- package.json | 21 +++++++-- test.js | 124 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 91 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index 8808f27..bc0d7db 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/test.js b/test.js index d7d6ee0..f9e6bc0 100644 --- a/test.js +++ b/test.js @@ -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); + }); + }); +});