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;
if(typeof module !== 'undefined' && module.exports && config.useNative) { // Use Node native crypto
if(util.detectNode() && config.useNative) { // Use Node native crypto
hash_fns = {
md5: node_hash('md5'),
sha1: node_hash('sha1'),

View File

@ -23,13 +23,11 @@
* @module crypto/random
*/
var type_mpi = require('../type/mpi.js'),
util = require('../util.js');
var nodeCrypto = null;
'use strict';
if (typeof window === 'undefined') {
nodeCrypto = require('crypto');
}
var TypeMPI = require('../type/mpi.js'),
util = require('../util.js'),
nodeCrypto = util.detectNode() && require('crypto');
module.exports = {
/**
@ -114,7 +112,7 @@ module.exports = {
randomBits.charCodeAt(0)) +
randomBits.substring(1);
}
var mpi = new type_mpi();
var mpi = new TypeMPI();
mpi.fromBytes(randomBits);
return mpi.toBigInteger();
},

View File

@ -32,6 +32,8 @@
* @module packet/sym_encrypted_integrity_protected
*/
'use strict';
module.exports = SymEncryptedIntegrityProtected;
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.
var version = bytes[0];
if (version != 1) {
if (version !== 1) {
throw new Error('Invalid packet version.');
}
@ -71,7 +73,6 @@ SymEncryptedIntegrityProtected.prototype.read = function (bytes) {
};
SymEncryptedIntegrityProtected.prototype.write = function () {
// 1 = Version
return util.concatUint8Array([new Uint8Array([1]), this.encrypted]);
};
@ -97,7 +98,7 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm
if(sessionKeyAlgorithm.substr(0,3) === 'aes') {
var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize;
// 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 Buffer = require('buffer').Buffer;
var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
@ -128,13 +129,12 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm
* @return {String} The decrypted data of this packet
*/
SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) {
var decrypted;
// AES optimizations. Native code for node, asmCrypto for browser.
if(sessionKeyAlgorithm.substr(0,3) === 'aes') {
var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize;
// 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 Buffer = require('buffer').Buffer;
var decipherObj = new nodeCrypto.createDecipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
@ -159,8 +159,9 @@ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm
var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length));
if (this.hash != mdc) {
if (this.hash !== mdc) {
throw new Error('Modification detected.');
} else
} else {
this.packets.read(decrypted.subarray(0, decrypted.length - 22));
}
};

View File

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