Cleanup node.js api detection
This commit is contained in:
parent
3919441bbc
commit
346aa8f11a
|
@ -38,8 +38,9 @@ module.exports = SymEncryptedIntegrityProtected;
|
||||||
|
|
||||||
var util = require('../util.js'),
|
var util = require('../util.js'),
|
||||||
crypto = require('../crypto'),
|
crypto = require('../crypto'),
|
||||||
config = require('../config'),
|
enums = require('../enums.js'),
|
||||||
enums = require('../enums.js');
|
nodeCrypto = util.getNodeCrypto(),
|
||||||
|
Buffer = util.getNodeBuffer();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
|
@ -98,15 +99,10 @@ 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(util.detectNode() && config.useNative) {
|
if(nodeCrypto) {
|
||||||
var nodeCrypto = require('crypto');
|
|
||||||
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',
|
|
||||||
// util.Uint8Array2str(key), util.Uint8Array2str(new Uint8Array(blockSize)));
|
|
||||||
//this.encrypted = new Uint8Array(cipherObj.update(util.Uint8Array2str(util.concatUint8Array([prefix, tohash]))));
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.encrypted = asmCrypto.AES_CFB.encrypt(util.concatUint8Array([prefix, tohash]), key);
|
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') {
|
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(util.detectNode() && config.useNative) {
|
if(nodeCrypto) {
|
||||||
var nodeCrypto = require('crypto');
|
|
||||||
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)));
|
||||||
|
|
27
src/util.js
27
src/util.js
|
@ -412,7 +412,6 @@ module.exports = {
|
||||||
*/
|
*/
|
||||||
getWebCrypto: function() {
|
getWebCrypto: function() {
|
||||||
if (!config.useNative) {
|
if (!config.useNative) {
|
||||||
// make web crypto optional
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -465,6 +464,32 @@ module.exports = {
|
||||||
*/
|
*/
|
||||||
detectNode: function() {
|
detectNode: function() {
|
||||||
return typeof window === 'undefined';
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user