From 4cabe075d5d67bde335d6f984ac30e6b77587da3 Mon Sep 17 00:00:00 2001 From: Robert Nelson Date: Thu, 9 Jan 2014 07:31:00 -0800 Subject: [PATCH] Add standalone support --- Gruntfile.js | 46 +++++++++++++++++++++++++++++++--- src/crypto/random.js | 8 +++--- test/crypto/cipher/aes.js | 5 ++-- test/crypto/cipher/blowfish.js | 5 ++-- test/crypto/cipher/cast5.js | 5 ++-- test/crypto/cipher/des.js | 5 ++-- test/crypto/cipher/twofish.js | 5 ++-- test/crypto/crypto.js | 5 ++-- test/crypto/hash/md5.js | 5 ++-- test/crypto/hash/ripemd.js | 5 ++-- test/crypto/hash/sha.js | 5 ++-- test/general/basic.js | 5 ++-- test/general/key.js | 5 ++-- test/general/keyring.js | 5 ++-- test/general/packet.js | 5 ++-- test/general/signature.js | 5 ++-- test/unittests.html | 2 +- 17 files changed, 89 insertions(+), 37 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 8968d7ce..0a5f3766 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -23,6 +23,25 @@ module.exports = function(grunt) { external: [ 'crypto', 'node-localstorage' ] } }, + openpgp_sa_nodebug: { + files: { + 'dist/openpgp-sa_nodebug.js': [ './src/index.js' ] + }, + options: { + standalone: 'openpgp', + external: [ 'crypto', 'node-localstorage' ] + } + }, + openpgp_sa: { + files: { + 'dist/openpgp-sa.js': [ './src/index.js' ] + }, + options: { + debug: true, + standalone: 'openpgp', + external: [ 'crypto', 'node-localstorage' ] + } + }, unittests: { files: { 'test/lib/unittests-bundle.js': [] @@ -35,7 +54,7 @@ module.exports = function(grunt) { } }, replace: { - openpgpjs: { + openpgp: { src: ['dist/openpgp.js'], dest: ['dist/openpgp.js'], replacements: [{ @@ -43,21 +62,42 @@ module.exports = function(grunt) { to: 'OpenPGP.js v<%= pkg.version %>.<%= grunt.template.today("yyyymmdd") %>' }] }, - openpgpjs_nodebug: { + openpgp_nodebug: { src: ['dist/openpgp_nodebug.js'], dest: ['dist/openpgp_nodebug.js'], replacements: [{ from: /OpenPGP.js VERSION/g, to: 'OpenPGP.js v<%= pkg.version %>.<%= grunt.template.today("yyyymmdd") %>' }] + }, + openpgp_sa: { + src: ['dist/openpgp-sa.js'], + dest: ['dist/openpgp-sa.js'], + replacements: [{ + from: /OpenPGP.js VERSION/g, + to: 'OpenPGP.js v<%= pkg.version %>.<%= grunt.template.today("yyyymmdd") %>' + }] + }, + openpgp_sa_nodebug: { + src: ['dist/openpgp-sa_nodebug.js'], + dest: ['dist/openpgp-sa_nodebug.js'], + replacements: [{ + from: /OpenPGP.js VERSION/g, + to: 'OpenPGP.js v<%= pkg.version %>.<%= grunt.template.today("yyyymmdd") %>' + }] } }, uglify: { - openpgpjs: { + openpgp: { files: { 'dist/openpgp.min.js' : [ 'dist/openpgp_nodebug.js' ] } }, + openpgp_sa: { + files: { + 'dist/openpgp-sa.min.js' : [ 'dist/openpgp-sa_nodebug.js' ] + } + }, options: { banner: '/*! OpenPGPjs.org this is LGPL licensed code, see LICENSE/our website for more information.- v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %> */' diff --git a/src/crypto/random.js b/src/crypto/random.js index c454e3a3..6f4b551d 100644 --- a/src/crypto/random.js +++ b/src/crypto/random.js @@ -25,10 +25,8 @@ var type_mpi = require('../type/mpi.js'); var nodeCrypto = null; -if (typeof window === undefined) {} -try { +if (typeof window === 'undefined') { nodeCrypto = require('crypto'); -} catch (e) { } module.exports = { @@ -81,9 +79,9 @@ module.exports = { * @param {Uint32Array} buf */ getRandomValues: function(buf) { - try { + if (nodeCrypto === null) { window.crypto.getRandomValues(buf); - } catch (e) { + } else { var bytes = nodeCrypto.randomBytes(4); buf[0] = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; } diff --git a/test/crypto/cipher/aes.js b/test/crypto/cipher/aes.js index 7b0f03ce..9b696586 100644 --- a/test/crypto/cipher/aes.js +++ b/test/crypto/cipher/aes.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/cipher/blowfish.js b/test/crypto/cipher/blowfish.js index 25d63792..1ad069ae 100644 --- a/test/crypto/cipher/blowfish.js +++ b/test/crypto/cipher/blowfish.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, BFencrypt = openpgp.crypto.cipher.blowfish, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/cipher/cast5.js b/test/crypto/cipher/cast5.js index 3cfc3301..fe2fe031 100644 --- a/test/crypto/cipher/cast5.js +++ b/test/crypto/cipher/cast5.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/cipher/des.js b/test/crypto/cipher/des.js index 4933f058..55d67be8 100644 --- a/test/crypto/cipher/des.js +++ b/test/crypto/cipher/des.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/cipher/twofish.js b/test/crypto/cipher/twofish.js index b002459c..ef5b979b 100644 --- a/test/crypto/cipher/twofish.js +++ b/test/crypto/cipher/twofish.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/crypto.js b/test/crypto/crypto.js index 45a79df5..1b731284 100644 --- a/test/crypto/crypto.js +++ b/test/crypto/crypto.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - chai = require('chai'), +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var chai = require('chai'), expect = chai.expect; describe('API functional testing', function() { diff --git a/test/crypto/hash/md5.js b/test/crypto/hash/md5.js index 39741f9b..8f67c1b4 100644 --- a/test/crypto/hash/md5.js +++ b/test/crypto/hash/md5.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, MD5 = openpgp.crypto.hash.md5, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/hash/ripemd.js b/test/crypto/hash/ripemd.js index b20baca0..5b2add1b 100644 --- a/test/crypto/hash/ripemd.js +++ b/test/crypto/hash/ripemd.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, RMDstring = openpgp.crypto.hash.ripemd, chai = require('chai'), expect = chai.expect; diff --git a/test/crypto/hash/sha.js b/test/crypto/hash/sha.js index d210ff50..33522008 100644 --- a/test/crypto/hash/sha.js +++ b/test/crypto/hash/sha.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - util = openpgp.util, +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var util = openpgp.util, hash = openpgp.crypto.hash, chai = require('chai'), expect = chai.expect; diff --git a/test/general/basic.js b/test/general/basic.js index a4db8482..2ffce81c 100644 --- a/test/general/basic.js +++ b/test/general/basic.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - chai = require('chai'), +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var chai = require('chai'), expect = chai.expect; describe('Basic', function() { diff --git a/test/general/key.js b/test/general/key.js index 758a0853..61b399bf 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - chai = require('chai'), +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var chai = require('chai'), expect = chai.expect; describe('Key', function() { diff --git a/test/general/keyring.js b/test/general/keyring.js index ccae3d36..5960e11d 100644 --- a/test/general/keyring.js +++ b/test/general/keyring.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - keyring = new openpgp.Keyring(), +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var keyring = new openpgp.Keyring(), chai = require('chai'), expect = chai.expect; diff --git a/test/general/packet.js b/test/general/packet.js index 975f2578..42a42ad7 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - chai = require('chai'), +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var chai = require('chai'), expect = chai.expect; describe("Packet", function() { diff --git a/test/general/signature.js b/test/general/signature.js index 5ca2007b..416b90c1 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -1,7 +1,8 @@ 'use strict'; -var openpgp = require('openpgp'), - chai = require('chai'), +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('openpgp'); + +var chai = require('chai'), expect = chai.expect; describe("Signature", function() { diff --git a/test/unittests.html b/test/unittests.html index 5c1093c8..06e46551 100644 --- a/test/unittests.html +++ b/test/unittests.html @@ -10,7 +10,7 @@
- +