diff --git a/Gruntfile.js b/Gruntfile.js index 14274ba5..658264e4 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -9,7 +9,8 @@ module.exports = function(grunt) { 'resources/openpgp_nodebug.js': [] }, options: { - alias: './src/:openpgp' + alias: './src/:openpgp', + external: [ 'crypto', 'node-localstorage' ] } }, openpgp: { @@ -18,7 +19,8 @@ module.exports = function(grunt) { }, options: { debug: true, - alias: './src/:openpgp' + alias: './src/:openpgp', + external: [ 'crypto', 'node-localstorage' ] } }, keyring_nodebug: { @@ -27,7 +29,7 @@ module.exports = function(grunt) { }, options: { alias: './src/keyring/:keyring', - external: [ 'openpgp' ] + external: [ 'openpgp', 'node-localstorage' ] } }, keyring: { @@ -37,7 +39,7 @@ module.exports = function(grunt) { options: { debug: true, alias: './src/keyring/:keyring', - external: [ 'openpgp' ] + external: [ 'openpgp', 'node-localstorage' ] } }, unittests: { @@ -47,7 +49,7 @@ module.exports = function(grunt) { options: { debug: true, alias: './test/unittests.js:unittests', - external: [ 'openpgp', 'keyring' ] + external: [ 'openpgp', 'keyring', 'node-localstorage' ] } } }, @@ -81,6 +83,8 @@ module.exports = function(grunt) { '<%= grunt.template.today("yyyy-mm-dd") %> */' } }, + prepare_install: { + }, jsbeautifier : { files : ["src/**/*.js"], options : { @@ -120,6 +124,7 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-text-replace'); + grunt.loadNpmTasks('grunt-prepare-install'); grunt.loadNpmTasks('grunt-jsbeautifier'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-jsdoc'); diff --git a/package.json b/package.json index cf23e6dc..8a8588cb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,11 @@ "engines": { "node": ">=0.8" }, - "main": "./src/index.js", + "directories": { + "lib": "src" + }, + "main": "src/index.js", + "files": [ "src" ], "scripts": { "pretest": "grunt", "test": "grunt test" diff --git a/src/crypto/random.js b/src/crypto/random.js index 774acda3..4497d9d0 100644 --- a/src/crypto/random.js +++ b/src/crypto/random.js @@ -23,6 +23,13 @@ */ var type_mpi = require('../type/mpi.js'); +var nodeCrypto = null; + +if (typeof window === undefined) {} +try { + crypto = require('crypto'); +} catch (e) { +} module.exports = { /** @@ -56,7 +63,7 @@ module.exports = { */ getSecureRandom: function(from, to) { var buf = new Uint32Array(1); - window.crypto.getRandomValues(buf); + this.getRandomValues(buf); var bits = ((to - from)).toString(2).length; while ((buf[0] & (Math.pow(2, bits) - 1)) > (to - from)) window.crypto.getRandomValues(buf); @@ -65,10 +72,23 @@ module.exports = { getSecureRandomOctet: function() { var buf = new Uint32Array(1); - window.crypto.getRandomValues(buf); + this.getRandomValues(buf); return buf[0] & 0xFF; }, + /** + * Helper routine which calls platform specific crypto random generator + * @param {Uint32Array} buf + */ + getRandomValues: function(buf) { + try { + window.crypto.getRandomValues(buf); + } catch (e) { + var bytes = crypto.randomBytes(4); + buf[0] = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; + } + }, + /** * Create a secure random big integer of bits length * @param {Integer} bits Bit length of the MPI to create diff --git a/src/keyring/localstore.js b/src/keyring/localstore.js index c9c18411..7d60b41d 100644 --- a/src/keyring/localstore.js +++ b/src/keyring/localstore.js @@ -32,7 +32,16 @@ function LocalStore() { * @return {Array} array of keys retrieved from localstore */ LocalStore.prototype.load = function () { - var armoredKeys = JSON.parse(window.localStorage.getItem("armoredKeys")); + var storage = null; + try { + storage = window.localStorage; + } catch (e) { + } + + if (storage === null) { + storage = new (require('node-localstorage').LocalStorage)('./keyring.store'); + } + var armoredKeys = JSON.parse(storage.getItem("armoredKeys")); var keys = []; if (armoredKeys !== null && armoredKeys.length !== 0) { var key; diff --git a/src/keyring/package.json b/src/keyring/package.json new file mode 100644 index 00000000..3740bd4c --- /dev/null +++ b/src/keyring/package.json @@ -0,0 +1,11 @@ +{ + "name": "keyring", + "version": "0.2.0", + "description": "Openpgpjs Keyring", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "BSD" +} diff --git a/test/crypto/cipher/aes.js b/test/crypto/cipher/aes.js index 62166a7b..7b0f03ce 100644 --- a/test/crypto/cipher/aes.js +++ b/test/crypto/cipher/aes.js @@ -2,6 +2,7 @@ var openpgp = require('openpgp'), util = openpgp.util, + chai = require('chai'), expect = chai.expect; describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function() { diff --git a/test/crypto/cipher/blowfish.js b/test/crypto/cipher/blowfish.js index 806e33cd..25d63792 100644 --- a/test/crypto/cipher/blowfish.js +++ b/test/crypto/cipher/blowfish.js @@ -3,6 +3,7 @@ var openpgp = require('openpgp'), util = openpgp.util, BFencrypt = openpgp.crypto.cipher.blowfish, + chai = require('chai'), expect = chai.expect; it('Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt', function(done) { diff --git a/test/crypto/cipher/cast5.js b/test/crypto/cipher/cast5.js index c1fc7344..3cfc3301 100644 --- a/test/crypto/cipher/cast5.js +++ b/test/crypto/cipher/cast5.js @@ -2,6 +2,7 @@ var openpgp = require('openpgp'), util = openpgp.util, + chai = require('chai'), expect = chai.expect; it('CAST-128 cipher test with test vectors from RFC2144', function (done) { diff --git a/test/crypto/cipher/des.js b/test/crypto/cipher/des.js index d002fe0b..4933f058 100644 --- a/test/crypto/cipher/des.js +++ b/test/crypto/cipher/des.js @@ -2,6 +2,7 @@ var openpgp = require('openpgp'), util = openpgp.util, + chai = require('chai'), expect = chai.expect; describe('TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf', function() { diff --git a/test/crypto/cipher/twofish.js b/test/crypto/cipher/twofish.js index 3b1dfd8f..b002459c 100644 --- a/test/crypto/cipher/twofish.js +++ b/test/crypto/cipher/twofish.js @@ -2,6 +2,7 @@ var openpgp = require('openpgp'), util = openpgp.util, + chai = require('chai'), expect = chai.expect; it('Twofish with test vectors from http://www.schneier.com/code/ecb_ival.txt', function(done) { diff --git a/test/crypto/crypto.js b/test/crypto/crypto.js index df455dc6..54911211 100644 --- a/test/crypto/crypto.js +++ b/test/crypto/crypto.js @@ -1,8 +1,8 @@ -var openpgp = require('openpgp'); - 'use strict'; -var expect = chai.expect; +var openpgp = require('openpgp'), + chai = require('chai'), + expect = chai.expect; describe('API functional testing', function() { var util = openpgp.util; diff --git a/test/crypto/hash/md5.js b/test/crypto/hash/md5.js index 39c46c83..39741f9b 100644 --- a/test/crypto/hash/md5.js +++ b/test/crypto/hash/md5.js @@ -3,6 +3,7 @@ var openpgp = require('openpgp'), util = openpgp.util, MD5 = openpgp.crypto.hash.md5, + chai = require('chai'), expect = chai.expect; it('MD5 with test vectors from RFC 1321', function(done) { diff --git a/test/crypto/hash/ripemd.js b/test/crypto/hash/ripemd.js index db947e3a..b20baca0 100644 --- a/test/crypto/hash/ripemd.js +++ b/test/crypto/hash/ripemd.js @@ -3,6 +3,7 @@ var openpgp = require('openpgp'), util = openpgp.util, RMDstring = openpgp.crypto.hash.ripemd, + chai = require('chai'), expect = chai.expect; it("RIPE-MD 160 bits with test vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html", function(done) { diff --git a/test/crypto/hash/sha.js b/test/crypto/hash/sha.js index 7bbdf4ea..d210ff50 100644 --- a/test/crypto/hash/sha.js +++ b/test/crypto/hash/sha.js @@ -3,6 +3,7 @@ var openpgp = require('openpgp'), util = openpgp.util, hash = openpgp.crypto.hash, + chai = require('chai'), expect = chai.expect; it('SHA* with test vectors from NIST FIPS 180-2', function(done) { diff --git a/test/general/basic.js b/test/general/basic.js index 37abb028..a4db8482 100644 --- a/test/general/basic.js +++ b/test/general/basic.js @@ -1,8 +1,8 @@ -var openpgp = require('openpgp'); - 'use strict'; -var expect = chai.expect; +var openpgp = require('openpgp'), + chai = require('chai'), + expect = chai.expect; describe('Basic', function() { diff --git a/test/general/key.js b/test/general/key.js index ce7e8d36..758a0853 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -1,8 +1,8 @@ -var openpgp = require('openpgp'); - 'use strict'; -var expect = chai.expect; +var openpgp = require('openpgp'), + chai = require('chai'), + expect = chai.expect; describe('Key', function() { var twoKeys = diff --git a/test/general/keyring.js b/test/general/keyring.js index 271c0df8..9dd51346 100644 --- a/test/general/keyring.js +++ b/test/general/keyring.js @@ -2,6 +2,7 @@ var openpgp = require('openpgp'), keyring = new (require('keyring'))(), + chai = require('chai'), expect = chai.expect; describe("Keyring", function() { diff --git a/test/general/packet.js b/test/general/packet.js index ab84d779..8a02d1f5 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -1,8 +1,8 @@ -var openpgp = require('openpgp'); - 'use strict'; -var expect = chai.expect; +var openpgp = require('openpgp'), + chai = require('chai'), + expect = chai.expect; describe("Packet", function() { var armored_key = diff --git a/test/general/signature.js b/test/general/signature.js index 0524a3e9..5ca2007b 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -1,8 +1,8 @@ -var openpgp = require('openpgp'); - 'use strict'; -var expect = chai.expect; +var openpgp = require('openpgp'), + chai = require('chai'), + expect = chai.expect; describe("Signature", function() { var priv_key_arm1 = diff --git a/test/package.json b/test/package.json new file mode 100644 index 00000000..c336713f --- /dev/null +++ b/test/package.json @@ -0,0 +1,20 @@ +{ + "name": "nodetests", + "version": "0.2.0", + "description": "Node.js server side unit tests", + "main": "example-test.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "BSD", + "devDependencies": { + "mocha": "~1.16.2", + "chai": "~1.8.1" + }, + "dependencies": { + "openpgp": "~0.2.0-dev", + "keyring": "~0.2.0", + "node-localstorage": "~0.3.4" + } +}