appjs-package2
Version:
packaged resource module with streaming support
54 lines (47 loc) • 1.64 kB
JavaScript
var crypto = require('crypto');
var util = require('util');
var stream = require('stream').Stream
/**
* base streaming class.
*/
function cryptoBaseStreamer(opts, cipher) {
//this._key = opts.key;
this._cipher = cipher;
this.inputEncoding = opts.inputEncoding;
this.outputEncoding = opts.outputEncoding;
this.readable = this.writable = true;
}
util.inherits(cryptoBaseStreamer, stream);
//exports.cryptoBaseStreamer = cryptoBaseStreamer;
cryptoBaseStreamer.prototype.write = function(data) {
this.emit("data", this._cipher.update(data, this.inputEncoding, this.outputEncoding));
return true
}
cryptoBaseStreamer.prototype.end = function(data) {
if (data) this.write(data)
this.emit("data", this._cipher.final(this.outputEncoding))
this.emit("end");
}
function coearseOpts (opts) {
return 'string' == typeof opts ? {key: opts, algorithm: 'aes192'} : opts
}
/**
* var encrypter = new encrypter({key:'hi'});
*/
var encrypter = function(opts) {
console.log("creating encrypter");
if (!opts) opts = {};
if (!opts.algorithm) opts.algorithm = 'aes192';
if (!opts.key) opts.key = '=4563%321!3244???';
return encrypter.super_.call(this, opts, crypto.createCipher(opts.algorithm, opts.key));
}
util.inherits(encrypter, cryptoBaseStreamer);
module.exports.Encrypter = encrypter;
var decrypter = function(opts) {
if (!opts) opts = {};
if (!opts.algorithm) opts.algorithm = 'aes192';
if (!opts.key) opts.key = '=4563%321!3244???';
return decrypter.super_.call(this, opts, crypto.createDecipher(opts.algorithm, opts.key));
}
util.inherits(decrypter, cryptoBaseStreamer);
module.exports.Decrypter = decrypter;