Cleanup node.js api detection

This commit is contained in:
Tankred Hase 2016-02-03 18:31:48 +07:00
parent 3919441bbc
commit 346aa8f11a
2 changed files with 32 additions and 13 deletions

View File

@ -38,8 +38,9 @@ module.exports = SymEncryptedIntegrityProtected;
var util = require('../util.js'),
crypto = require('../crypto'),
config = require('../config'),
enums = require('../enums.js');
enums = require('../enums.js'),
nodeCrypto = util.getNodeCrypto(),
Buffer = util.getNodeBuffer();
/**
* @constructor
@ -98,15 +99,10 @@ 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(util.detectNode() && config.useNative) {
var nodeCrypto = require('crypto');
var Buffer = require('buffer').Buffer;
if(nodeCrypto) {
var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
new Buffer(key), new Buffer(new Uint8Array(blockSize)));
this.encrypted = new Uint8Array(cipherObj.update(new Buffer(util.concatUint8Array([prefix, tohash]))));
//var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
// util.Uint8Array2str(key), util.Uint8Array2str(new Uint8Array(blockSize)));
//this.encrypted = new Uint8Array(cipherObj.update(util.Uint8Array2str(util.concatUint8Array([prefix, tohash]))));
}
else {
this.encrypted = asmCrypto.AES_CFB.encrypt(util.concatUint8Array([prefix, tohash]), key);
@ -134,9 +130,7 @@ SymEncryptedIntegrityProtected.prototype.decrypt = 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(util.detectNode() && config.useNative) {
var nodeCrypto = require('crypto');
var Buffer = require('buffer').Buffer;
if(nodeCrypto) {
var decipherObj = new nodeCrypto.createDecipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb',
new Buffer(key), new Buffer(new Uint8Array(blockSize)));
decrypted = new Uint8Array(decipherObj.update(new Buffer(this.encrypted)));

View File

@ -408,11 +408,10 @@ module.exports = {
/**
* Get native Web Cryptography api. The default configuration is to use
* the api when available. But it can also be deactivated with config.useNative
* @return {Object} The SubtleCrypto api or 'undefined'
* @return {Object} The SubtleCrypto api or 'undefined'
*/
getWebCrypto: function() {
if (!config.useNative) {
// make web crypto optional
return;
}
@ -465,6 +464,32 @@ module.exports = {
*/
detectNode: function() {
return typeof window === 'undefined';
},
/**
* Get native Node.js crypto api. The default configuration is to use
* the api when available. But it can also be deactivated with config.useNative
* @return {Object} The crypto module or 'undefined'
*/
getNodeCrypto: function() {
if (!this.detectNode() || !config.useNative) {
return;
}
return require('crypto');
},
/**
* Get native Node.js Buffer constructor. This should be used since
* Buffer is not available under browserify.
* @return {Function} The Buffer constructor or 'undefined'
*/
getNodeBuffer: function() {
if (!this.detectNode()) {
return;
}
return require('buffer').Buffer;
}
};