Add node.js support

This commit is contained in:
Robert Nelson 2014-01-08 09:01:15 -08:00
parent 82fbc0ffa7
commit 57c98386f9
20 changed files with 102 additions and 24 deletions

View File

@ -9,7 +9,8 @@ module.exports = function(grunt) {
'resources/openpgp_nodebug.js': [] 'resources/openpgp_nodebug.js': []
}, },
options: { options: {
alias: './src/:openpgp' alias: './src/:openpgp',
external: [ 'crypto', 'node-localstorage' ]
} }
}, },
openpgp: { openpgp: {
@ -18,7 +19,8 @@ module.exports = function(grunt) {
}, },
options: { options: {
debug: true, debug: true,
alias: './src/:openpgp' alias: './src/:openpgp',
external: [ 'crypto', 'node-localstorage' ]
} }
}, },
keyring_nodebug: { keyring_nodebug: {
@ -27,7 +29,7 @@ module.exports = function(grunt) {
}, },
options: { options: {
alias: './src/keyring/:keyring', alias: './src/keyring/:keyring',
external: [ 'openpgp' ] external: [ 'openpgp', 'node-localstorage' ]
} }
}, },
keyring: { keyring: {
@ -37,7 +39,7 @@ module.exports = function(grunt) {
options: { options: {
debug: true, debug: true,
alias: './src/keyring/:keyring', alias: './src/keyring/:keyring',
external: [ 'openpgp' ] external: [ 'openpgp', 'node-localstorage' ]
} }
}, },
unittests: { unittests: {
@ -47,7 +49,7 @@ module.exports = function(grunt) {
options: { options: {
debug: true, debug: true,
alias: './test/unittests.js:unittests', 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") %> */' '<%= grunt.template.today("yyyy-mm-dd") %> */'
} }
}, },
prepare_install: {
},
jsbeautifier : { jsbeautifier : {
files : ["src/**/*.js"], files : ["src/**/*.js"],
options : { options : {
@ -120,6 +124,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-text-replace'); grunt.loadNpmTasks('grunt-text-replace');
grunt.loadNpmTasks('grunt-prepare-install');
grunt.loadNpmTasks('grunt-jsbeautifier'); grunt.loadNpmTasks('grunt-jsbeautifier');
grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-jsdoc'); grunt.loadNpmTasks('grunt-jsdoc');

View File

@ -4,7 +4,11 @@
"engines": { "engines": {
"node": ">=0.8" "node": ">=0.8"
}, },
"main": "./src/index.js", "directories": {
"lib": "src"
},
"main": "src/index.js",
"files": [ "src" ],
"scripts": { "scripts": {
"pretest": "grunt", "pretest": "grunt",
"test": "grunt test" "test": "grunt test"

View File

@ -23,6 +23,13 @@
*/ */
var type_mpi = require('../type/mpi.js'); var type_mpi = require('../type/mpi.js');
var nodeCrypto = null;
if (typeof window === undefined) {}
try {
crypto = require('crypto');
} catch (e) {
}
module.exports = { module.exports = {
/** /**
@ -56,7 +63,7 @@ module.exports = {
*/ */
getSecureRandom: function(from, to) { getSecureRandom: function(from, to) {
var buf = new Uint32Array(1); var buf = new Uint32Array(1);
window.crypto.getRandomValues(buf); this.getRandomValues(buf);
var bits = ((to - from)).toString(2).length; var bits = ((to - from)).toString(2).length;
while ((buf[0] & (Math.pow(2, bits) - 1)) > (to - from)) while ((buf[0] & (Math.pow(2, bits) - 1)) > (to - from))
window.crypto.getRandomValues(buf); window.crypto.getRandomValues(buf);
@ -65,10 +72,23 @@ module.exports = {
getSecureRandomOctet: function() { getSecureRandomOctet: function() {
var buf = new Uint32Array(1); var buf = new Uint32Array(1);
window.crypto.getRandomValues(buf); this.getRandomValues(buf);
return buf[0] & 0xFF; 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 * Create a secure random big integer of bits length
* @param {Integer} bits Bit length of the MPI to create * @param {Integer} bits Bit length of the MPI to create

View File

@ -32,7 +32,16 @@ function LocalStore() {
* @return {Array<module:key~Key>} array of keys retrieved from localstore * @return {Array<module:key~Key>} array of keys retrieved from localstore
*/ */
LocalStore.prototype.load = function () { 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 = []; var keys = [];
if (armoredKeys !== null && armoredKeys.length !== 0) { if (armoredKeys !== null && armoredKeys.length !== 0) {
var key; var key;

11
src/keyring/package.json Normal file
View File

@ -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"
}

View File

@ -2,6 +2,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function() { describe('AES Rijndael cipher test with test vectors from ecb_tbl.txt', function() {

View File

@ -3,6 +3,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
BFencrypt = openpgp.crypto.cipher.blowfish, BFencrypt = openpgp.crypto.cipher.blowfish,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
it('Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt', function(done) { it('Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt', function(done) {

View File

@ -2,6 +2,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
it('CAST-128 cipher test with test vectors from RFC2144', function (done) { it('CAST-128 cipher test with test vectors from RFC2144', function (done) {

View File

@ -2,6 +2,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
describe('TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf', function() { describe('TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf', function() {

View File

@ -2,6 +2,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
it('Twofish with test vectors from http://www.schneier.com/code/ecb_ival.txt', function(done) { it('Twofish with test vectors from http://www.schneier.com/code/ecb_ival.txt', function(done) {

View File

@ -1,8 +1,8 @@
var openpgp = require('openpgp');
'use strict'; 'use strict';
var expect = chai.expect; var openpgp = require('openpgp'),
chai = require('chai'),
expect = chai.expect;
describe('API functional testing', function() { describe('API functional testing', function() {
var util = openpgp.util; var util = openpgp.util;

View File

@ -3,6 +3,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
MD5 = openpgp.crypto.hash.md5, MD5 = openpgp.crypto.hash.md5,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
it('MD5 with test vectors from RFC 1321', function(done) { it('MD5 with test vectors from RFC 1321', function(done) {

View File

@ -3,6 +3,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
RMDstring = openpgp.crypto.hash.ripemd, RMDstring = openpgp.crypto.hash.ripemd,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
it("RIPE-MD 160 bits with test vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html", function(done) { it("RIPE-MD 160 bits with test vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html", function(done) {

View File

@ -3,6 +3,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
util = openpgp.util, util = openpgp.util,
hash = openpgp.crypto.hash, hash = openpgp.crypto.hash,
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
it('SHA* with test vectors from NIST FIPS 180-2', function(done) { it('SHA* with test vectors from NIST FIPS 180-2', function(done) {

View File

@ -1,8 +1,8 @@
var openpgp = require('openpgp');
'use strict'; 'use strict';
var expect = chai.expect; var openpgp = require('openpgp'),
chai = require('chai'),
expect = chai.expect;
describe('Basic', function() { describe('Basic', function() {

View File

@ -1,8 +1,8 @@
var openpgp = require('openpgp');
'use strict'; 'use strict';
var expect = chai.expect; var openpgp = require('openpgp'),
chai = require('chai'),
expect = chai.expect;
describe('Key', function() { describe('Key', function() {
var twoKeys = var twoKeys =

View File

@ -2,6 +2,7 @@
var openpgp = require('openpgp'), var openpgp = require('openpgp'),
keyring = new (require('keyring'))(), keyring = new (require('keyring'))(),
chai = require('chai'),
expect = chai.expect; expect = chai.expect;
describe("Keyring", function() { describe("Keyring", function() {

View File

@ -1,8 +1,8 @@
var openpgp = require('openpgp');
'use strict'; 'use strict';
var expect = chai.expect; var openpgp = require('openpgp'),
chai = require('chai'),
expect = chai.expect;
describe("Packet", function() { describe("Packet", function() {
var armored_key = var armored_key =

View File

@ -1,8 +1,8 @@
var openpgp = require('openpgp');
'use strict'; 'use strict';
var expect = chai.expect; var openpgp = require('openpgp'),
chai = require('chai'),
expect = chai.expect;
describe("Signature", function() { describe("Signature", function() {
var priv_key_arm1 = var priv_key_arm1 =

20
test/package.json Normal file
View File

@ -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"
}
}