Fix node.js detection

This commit is contained in:
Tankred Hase 2016-02-03 16:05:25 +07:00
parent 00ea3918c5
commit 68d298c948
4 changed files with 32 additions and 26 deletions

View File

@ -23,7 +23,7 @@ function node_hash(type) {
} }
var hash_fns; var hash_fns;
if(typeof module !== 'undefined' && module.exports && config.useNative) { // Use Node native crypto if(util.detectNode() && config.useNative) { // Use Node native crypto
hash_fns = { hash_fns = {
md5: node_hash('md5'), md5: node_hash('md5'),
sha1: node_hash('sha1'), sha1: node_hash('sha1'),

View File

@ -1,19 +1,19 @@
// GPG4Browsers - An OpenPGP implementation in javascript // GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH // Copyright (C) 2011 Recurity Labs GmbH
// //
// This library is free software; you can redistribute it and/or // This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public // modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either // License as published by the Free Software Foundation; either
// version 3.0 of the License, or (at your option) any later version. // version 3.0 of the License, or (at your option) any later version.
// //
// This library is distributed in the hope that it will be useful, // This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details. // Lesser General Public License for more details.
// //
// You should have received a copy of the GNU Lesser General Public // You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software // License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// The GPG4Browsers crypto interface // The GPG4Browsers crypto interface
@ -23,13 +23,11 @@
* @module crypto/random * @module crypto/random
*/ */
var type_mpi = require('../type/mpi.js'), 'use strict';
util = require('../util.js');
var nodeCrypto = null;
if (typeof window === 'undefined') { var TypeMPI = require('../type/mpi.js'),
nodeCrypto = require('crypto'); util = require('../util.js'),
} nodeCrypto = util.detectNode() && require('crypto');
module.exports = { module.exports = {
/** /**
@ -114,7 +112,7 @@ module.exports = {
randomBits.charCodeAt(0)) + randomBits.charCodeAt(0)) +
randomBits.substring(1); randomBits.substring(1);
} }
var mpi = new type_mpi(); var mpi = new TypeMPI();
mpi.fromBytes(randomBits); mpi.fromBytes(randomBits);
return mpi.toBigInteger(); return mpi.toBigInteger();
}, },

View File

@ -1,16 +1,16 @@
// GPG4Browsers - An OpenPGP implementation in javascript // GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH // Copyright (C) 2011 Recurity Labs GmbH
// //
// This library is free software; you can redistribute it and/or // This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public // modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either // License as published by the Free Software Foundation; either
// version 3.0 of the License, or (at your option) any later version. // version 3.0 of the License, or (at your option) any later version.
// //
// This library is distributed in the hope that it will be useful, // This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details. // Lesser General Public License for more details.
// //
// You should have received a copy of the GNU Lesser General Public // You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software // License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
@ -32,6 +32,8 @@
* @module packet/sym_encrypted_integrity_protected * @module packet/sym_encrypted_integrity_protected
*/ */
'use strict';
module.exports = SymEncryptedIntegrityProtected; module.exports = SymEncryptedIntegrityProtected;
var util = require('../util.js'), var util = require('../util.js'),
@ -60,7 +62,7 @@ SymEncryptedIntegrityProtected.prototype.read = function (bytes) {
// - A one-octet version number. The only currently defined value is 1. // - A one-octet version number. The only currently defined value is 1.
var version = bytes[0]; var version = bytes[0];
if (version != 1) { if (version !== 1) {
throw new Error('Invalid packet version.'); throw new Error('Invalid packet version.');
} }
@ -71,7 +73,6 @@ SymEncryptedIntegrityProtected.prototype.read = function (bytes) {
}; };
SymEncryptedIntegrityProtected.prototype.write = function () { SymEncryptedIntegrityProtected.prototype.write = function () {
// 1 = Version // 1 = Version
return util.concatUint8Array([new Uint8Array([1]), this.encrypted]); return util.concatUint8Array([new Uint8Array([1]), this.encrypted]);
}; };
@ -97,13 +98,13 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm
if(sessionKeyAlgorithm.substr(0,3) === 'aes') { if(sessionKeyAlgorithm.substr(0,3) === 'aes') {
var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize; var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize;
// Node crypto library. Not clear that it is faster than asmCrypto // Node crypto library. Not clear that it is faster than asmCrypto
if(typeof module !== 'undefined' && module.exports && config.useNative) { if(util.detectNode() && config.useNative) {
var nodeCrypto = require('crypto'); var nodeCrypto = require('crypto');
var Buffer = require('buffer').Buffer; var Buffer = require('buffer').Buffer;
var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
new Buffer(key), new Buffer(new Uint8Array(blockSize))); new Buffer(key), new Buffer(new Uint8Array(blockSize)));
this.encrypted = new Uint8Array(cipherObj.update(new Buffer(util.concatUint8Array([prefix, tohash])))); this.encrypted = new Uint8Array(cipherObj.update(new Buffer(util.concatUint8Array([prefix, tohash]))));
//var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', //var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
// util.Uint8Array2str(key), util.Uint8Array2str(new Uint8Array(blockSize))); // util.Uint8Array2str(key), util.Uint8Array2str(new Uint8Array(blockSize)));
//this.encrypted = new Uint8Array(cipherObj.update(util.Uint8Array2str(util.concatUint8Array([prefix, tohash])))); //this.encrypted = new Uint8Array(cipherObj.update(util.Uint8Array2str(util.concatUint8Array([prefix, tohash]))));
} }
@ -128,16 +129,15 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm
* @return {String} The decrypted data of this packet * @return {String} The decrypted data of this packet
*/ */
SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) { SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) {
var decrypted; var decrypted;
// AES optimizations. Native code for node, asmCrypto for browser. // AES optimizations. Native code for node, asmCrypto for browser.
if(sessionKeyAlgorithm.substr(0,3) === 'aes') { if(sessionKeyAlgorithm.substr(0,3) === 'aes') {
var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize; var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize;
// Node crypto library. Not clear that it is faster than asmCrypto // Node crypto library. Not clear that it is faster than asmCrypto
if(typeof module !== 'undefined' && module.exports && config.useNative) { if(util.detectNode() && config.useNative) {
var nodeCrypto = require('crypto'); var nodeCrypto = require('crypto');
var Buffer = require('buffer').Buffer; var Buffer = require('buffer').Buffer;
var decipherObj = new nodeCrypto.createDecipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', var decipherObj = new nodeCrypto.createDecipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
new Buffer(key), new Buffer(new Uint8Array(blockSize))); new Buffer(key), new Buffer(new Uint8Array(blockSize)));
decrypted = new Uint8Array(decipherObj.update(new Buffer(this.encrypted))); decrypted = new Uint8Array(decipherObj.update(new Buffer(this.encrypted)));
} }
@ -154,13 +154,14 @@ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm
// there must be a modification detection code packet as the // there must be a modification detection code packet as the
// last packet and everything gets hashed except the hash itself // last packet and everything gets hashed except the hash itself
this.hash = util.Uint8Array2str(crypto.hash.sha1(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted), this.hash = util.Uint8Array2str(crypto.hash.sha1(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted),
decrypted.subarray(0, decrypted.length - 20)]))); decrypted.subarray(0, decrypted.length - 20)])));
var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length)); var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length));
if (this.hash != mdc) { if (this.hash !== mdc) {
throw new Error('Modification detected.'); throw new Error('Modification detected.');
} else } else {
this.packets.read(decrypted.subarray(0, decrypted.length - 22)); this.packets.read(decrypted.subarray(0, decrypted.length - 22));
}
}; };

View File

@ -458,6 +458,13 @@ module.exports = {
resolve(e.target.result); resolve(e.target.result);
}; };
}); });
},
/**
* Detect Node.js runtime.
*/
detectNode: function() {
return typeof window === 'undefined';
} }
}; };