Socket
Socket
Sign inDemoInstall

node-secure-stream

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-secure-stream - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

__tests__/basic_decryption_test.js

7

CHANGES.md
## Change log
### Version 0.1.1 - 13th December 2018
* No API changes at all
* Dropped CoffeeScript build process
* Switched to Jest for testing
### Version 0.1.0 - 1st March 2017
* Initial release

214

lib/index.js

@@ -1,15 +0,16 @@

// Generated by CoffeeScript 1.12.4
var Decrypter, Encrypter, Transform, crypto,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const crypto = require('crypto');
crypto = require('crypto');
const { Transform } = require('stream');
Transform = require('stream').Transform;
class Encrypter extends Transform {
Encrypter = (function(superClass) {
extend(Encrypter, superClass);
constructor(options) {
super(options);
function Encrypter(options) {
Encrypter.__super__.constructor.call(this, options);
this.header_written = false;

@@ -26,2 +27,3 @@ this.algorithm = options.algorithm;

this.key = options.key;
if (!this.public_key) {

@@ -32,22 +34,31 @@ throw new Error("Missing public key");

Encrypter.prototype.getRandomBytes = function(length) {
getRandomBytes(length) {
return crypto.randomBytes(length);
};
}
Encrypter.prototype.initialize = function() {
var encrypted_key, header, index, iv, key, keys, self;
self = this;
keys = this.getRandomBytes(16 + this.key_length / 8);
iv = keys.slice(0, 16);
key = keys.slice(16);
initialize() {
const self = this;
const keys = this.getRandomBytes(16 + (this.key_length/8));
const iv = keys.slice(0, 16);
const key = keys.slice(16);
// logger.debug "KEY", key
// logger.debug "IV", iv
// Make a regular cipher object. This handles the writing of all
// subsequent data.
// logger.debug "initialize createCipheriv", @algorithm, key, iv
this.cipher = crypto.createCipheriv(this.algorithm, key, iv);
this.cipher.on('data', function(buffer) {
return self.push(buffer);
});
this.cipher.on('error', function(error) {
return logger.debug('cipher error', error);
});
encrypted_key = crypto.publicEncrypt(this.public_key, key);
header = Buffer.alloc(4096);
index = 0;
this.cipher.on('data', buffer =>
// logger.debug 'cipher data', buffer
self.push(buffer)
);
this.cipher.on('error', error => logger.debug('cipher error', error));
// Now, we need to manage the RSA handling of the AES key.
const encrypted_key = crypto.publicEncrypt(this.public_key, key);
const header = Buffer.alloc(4096);
let index = 0;
index = header.writeInt16LE(0, index);

@@ -62,32 +73,43 @@ index = header.writeInt16LE(this.algorithm.length, index);

index = index + iv.length;
// Write the count back
header.writeInt16LE(index, 0);
this.push(header.slice(0, index));
return this.header_written = true;
};
this.header_written = true;
}
Encrypter.prototype._transform = function(chunk, encoding, callback) {
var result;
_transform(chunk, encoding, callback) {
let result;
if (!this.header_written) {
// We've not written a header yet, so we make one and then send it
// to the output.
this.initialize();
}
return result = this.cipher.write(chunk, encoding, function() {
return callback();
});
};
Encrypter.prototype._flush = function(callback) {
return this.cipher.end(function() {
return callback();
});
};
// logger.debug "Encrypter _write", chunk, encoding
this.cipher.write(chunk, encoding, () =>
// logger.debug "Encrypter write cipher result", result
callback()
);
}
return Encrypter;
})(Transform);
_flush(callback) {
// logger.debug "Encrypter _flush"
this.cipher.end(() =>
// logger.debug "Encrypter _flush complete"
callback()
);
}
}
Decrypter = (function(superClass) {
extend(Decrypter, superClass);
function Decrypter(options) {
Decrypter.__super__.constructor.call(this, options);
class Decrypter extends Transform {
constructor(options) {
super(options);
this.header_complete = false;

@@ -100,68 +122,98 @@ this.key = options.key;

Decrypter.prototype.unpackHeader = function() {
var algorithm_size, encrypted_key, encrypted_key_size, index, iv_size, self;
self = this;
index = 0;
unpackHeader() {
const self = this;
let index = 0;
this.header_size = this.header.readInt16LE(index);
// logger.debug "unpackHeader @header_size", @header_size
index = index + 2;
algorithm_size = this.header.readInt16LE(index);
const algorithm_size = this.header.readInt16LE(index);
// logger.debug "unpackHeader algorithm_size", algorithm_size
index = index + 2;
this.algorithm = this.header.slice(index, index + algorithm_size).toString('latin1');
// logger.debug "unpackHeader @algorithm", @algorithm
index = index + algorithm_size;
encrypted_key_size = this.header.readInt16LE(index);
const encrypted_key_size = this.header.readInt16LE(index);
// logger.debug "unpackHeader encrypted_key_size", encrypted_key_size
index = index + 2;
encrypted_key = this.header.slice(index, index + encrypted_key_size);
const encrypted_key = this.header.slice(index, index + encrypted_key_size);
// logger.debug "unpackHeader encrypted_key", encrypted_key
index = index + encrypted_key_size;
iv_size = this.header.readInt16LE(index);
const iv_size = this.header.readInt16LE(index);
// logger.debug "unpackHeader iv_size", iv_size
index = index + 2;
this.iv = this.header.slice(index, index + iv_size);
// logger.debug "unpackHeader @iv", @iv
index = index + iv_size;
//# Now, let's decrypted the key, and build a decryption cipher
this.key = crypto.privateDecrypt(this.key, encrypted_key);
// logger.debug "unpackHeader decrypted", encrypted_key, 'to', @key
//# And here's the new cipher
// logger.debug "unpackHeader createDecipheriv", @algorithm, @key, @iv
this.cipher = crypto.createDecipheriv(this.algorithm, this.key, this.iv);
this.cipher.on('data', function(buffer) {
return self.push(buffer);
});
this.cipher.on('error', function(error) {
return logger.debug('cipher error', error);
});
return this.header_complete = true;
};
this.cipher.on('data', buffer =>
// logger.debug 'cipher data', buffer
self.push(buffer)
);
this.cipher.on('error', error => logger.debug('cipher error', error));
Decrypter.prototype._transform = function(chunk, encoding, callback) {
var header_buffer, result;
// logger.debug "unpackHeader done"
this.header_complete = true;
}
_transform(chunk, encoding, callback) {
let result;
chunk = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
// logger.debug "Got chunk", chunk, encoding
if (!this.header_complete) {
this.header.fill(chunk, this.header_index);
this.header_index = this.header_index + chunk.length;
// logger.debug "Added chunk", @header_index, chunk
if (this.header_index >= 2) {
this.header_size = this.header.readInt16LE(0);
// logger.debug "Worked out @header_size", @header_size
} else {
return callback();
callback();
return;
}
if ((this.header_size == null) || this.header_index < this.header_size) {
return callback();
if ((this.header_size == null) || (this.header_index < this.header_size)) {
callback();
return;
}
header_buffer = this.header.slice(0, this.header_size);
// logger.debug 'XXXX', @header_size, @header_index, chunk.length
const header_buffer = this.header.slice(0, this.header_size);
chunk = chunk.slice(chunk.length - (this.header_index - this.header_size));
this.header = header_buffer;
this.unpackHeader();
}
return result = this.cipher.write(chunk, encoding, function() {
return callback();
});
};
Decrypter.prototype._flush = function(callback) {
return this.cipher.end(function() {
return callback();
});
};
// logger.debug "Remaining", chunk, chunk.length
return Decrypter;
//# We might well have a bit of chunk left over, so if we do, let's
//# chop if off and run it through the cipher. This isn't just a block
//# after 4096, it depends on the header block size.
})(Transform);
this.cipher.write(chunk, encoding, () => callback());
}
_flush(callback) {
return this.cipher.end(() => callback());
}
}
module.exports = {
Encrypter: Encrypter,
Decrypter: Decrypter
Encrypter,
Decrypter
};
{
"name": "node-secure-stream",
"version": "0.1.0",
"version": "0.1.1",
"description": "Stream-based encryption for large amounts of data",

@@ -16,7 +16,5 @@ "main": "lib/index.js",

"devDependencies": {
"chai": "^3.5.0",
"coffee-script": "^1.10.0",
"concat-stream": "^1.6.0",
"log4js": "^1.1.0",
"mocha": "^3.0.2",
"jest": "^23.6.0",
"log4js": "^3.0.6",
"string-to-stream": "^1.1.0"

@@ -29,3 +27,18 @@ },

},
"homepage": "https://github.com/morungos/node-secure-stream#readme"
"homepage": "https://github.com/morungos/node-secure-stream#readme",
"jest": {
"moduleFileExtensions": [
"js"
],
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
],
"testRegex": "(/__tests__/.*)test\\.js$",
"collectCoverageFrom": [
"lib/**/*.js"
],
"coveragePathIgnorePatterns": [
"/node_modules/"
]
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc