3s
Version:
A Command-line script to triplesec encrypt and decrypt
480 lines (455 loc) • 16.6 kB
JavaScript
// Generated by IcedCoffeeScript 1.7.0-a
(function() {
var Cmd, decrypt, encrypt, fs, iced, main, make_esc, path, read, strip, usage, __iced_k, __iced_k_noop, _ref;
iced = require('iced-coffee-script').iced;
__iced_k = __iced_k_noop = function() {};
_ref = require('triplesec'), encrypt = _ref.encrypt, decrypt = _ref.decrypt;
read = require('read');
make_esc = require('iced-error').make_esc;
fs = require('fs');
path = require('path');
strip = function(s) {
return s.split(/\s+/).join('');
};
usage = function() {
return console.error("" + (path.basename(process.argv[1])) + " [opts] <enc|dec> [file]\n\nOptions:\n -k/--key <key> the key to use\n -o/--output <file> the file to output to\n -i/--input-encoding <enc> the input encoding to use\n -e/--output-encoding <enc> the output encoding to write with\n\nIf no file given, will read from stdin. If no output file, will write to standard\noutput. You can't clobber up standard input, so you can either prompt\nfor a key or pipe data in, but not both. I realize there are ways to\nsolve this, but I am lazy!");
};
Cmd = (function() {
function Cmd() {
this.filename = null;
this.cmd = null;
this._do_enc = false;
}
Cmd.prototype.check_encoding = function(e) {
if (!e || (e === 'hex' || e === 'base64' || e === 'binary' || e === 'none')) {
return null;
} else {
return new Error("bad encoding " + e);
}
};
Cmd.prototype.get_encoding = function(e) {
if (!e || (e === 'binary' || e === 'none')) {
return null;
} else {
return e;
}
};
Cmd.prototype.parse_args = function(cb) {
var cmd, err;
err = null;
this.argv = require('optimist').alias('k', 'key').alias('o', 'output').alias('i', 'input-encoding').alias('h', 'help').boolean('h').alias('e', 'output-encoding').argv;
if (this.argv.h) {
} else if (this.argv._.length < 1) {
err = new Error('Need an enc/dec comand');
} else if (this.argv._.length > 2) {
err = new Error('Can only handle 2 arguments at most');
} else {
if (this.argv._.length === 2) {
this.filename = this.argv._[1];
}
cmd = this.argv._[0];
if (cmd === 'enc' || cmd === 'encrypt') {
this.cmd = encrypt;
this._do_enc = true;
} else if (cmd === 'dec' || cmd === 'decrypt') {
this.cmd = decrypt;
} else {
err = new Error('Can only support enc or dec');
}
}
if (err == null) {
err = this.check_encoding(this.argv.i);
}
if (err == null) {
err = this.check_encoding(this.argv.e);
}
if (!(err != null) && !this.argv.h && !this.filename && !this.argv.k) {
err = new Error("Can't read a key and the data from standard input!");
}
return cb(err);
};
Cmd.prototype.get_input = function(cb) {
var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
(function(_this) {
return (function(__iced_k) {
if (_this.filename) {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.get_input"
});
fs.readFile(_this.filename, __iced_deferrals.defer({
assign_fn: (function(__slot_1) {
return function() {
err = arguments[0];
return __slot_1.data = arguments[1];
};
})(_this),
lineno: 87
}));
__iced_deferrals._fulfill();
})(__iced_k);
} else {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.get_input"
});
_this.consume_stdin(__iced_deferrals.defer({
assign_fn: (function(__slot_1) {
return function() {
err = arguments[0];
return __slot_1.data = arguments[1];
};
})(_this),
lineno: 89
}));
__iced_deferrals._fulfill();
})(__iced_k);
}
});
})(this)((function(_this) {
return function() {
return cb(err);
};
})(this));
};
Cmd.prototype.consume_stdin = function(cb) {
var bufs, fin, s;
bufs = [];
s = process.stdin;
fin = function(err, data) {
var tcb;
if (cb) {
tcb = cb;
cb = null;
return tcb(err, data);
}
};
s.resume();
s.on('data', function(buf) {
return bufs.push(buf);
});
s.on('err', function(err) {
return fin(err, null);
});
return s.on('end', function() {
return fin(null, Buffer.concat(bufs));
});
};
Cmd.prototype.get_key = function(cb) {
var err, match, p2, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
this.key = err = null;
(function(_this) {
return (function(__iced_k) {
if ((_this.key = _this.argv.k) == null) {
(function(__iced_k) {
var _results, _while;
_results = [];
_while = function(__iced_k) {
var _break, _continue, _next;
_break = function() {
return __iced_k(_results);
};
_continue = function() {
return iced.trampoline(function() {
return _while(__iced_k);
});
};
_next = function(__iced_next_arg) {
_results.push(__iced_next_arg);
return _continue();
};
if (!true) {
return _break();
} else {
match = false;
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.get_key"
});
read({
prompt: "password ------->",
silent: true,
replace: '*'
}, __iced_deferrals.defer({
assign_fn: (function(__slot_1) {
return function() {
err = arguments[0];
return __slot_1.key = arguments[1];
};
})(_this),
lineno: 117
}));
__iced_deferrals._fulfill();
})(function() {
(function(__iced_k) {
if (!(err != null) && _this._do_enc) {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.get_key"
});
read({
prompt: "password (again)>",
silent: true,
replace: '*'
}, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return p2 = arguments[1];
};
})(),
lineno: 119
}));
__iced_deferrals._fulfill();
})(function() {
return __iced_k(err == null ? match = _this.key === p2 : void 0);
});
} else {
return __iced_k();
}
})(function() {
(function(__iced_k) {
if (match || err || !_this._do_enc) {
(function(__iced_k) {
_break()
})(__iced_k);
} else {
return __iced_k();
}
})(_next);
});
});
}
};
_while(__iced_k);
})(__iced_k);
} else {
return __iced_k();
}
});
})(this)((function(_this) {
return function() {
return cb(err, _this.key);
};
})(this));
};
Cmd.prototype.get_in_data = function() {
var e, ret, s;
if ((e = this.get_encoding(this.argv.i)) != null) {
s = strip(this.data.toString('utf8'));
ret = new Buffer(s, e);
} else {
ret = this.data;
}
return ret;
};
Cmd.prototype.do_cmd = function(cb) {
var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.do_cmd"
});
_this.cmd({
key: new Buffer(_this.key, 'utf8'),
data: _this.get_in_data()
}, __iced_deferrals.defer({
assign_fn: (function(__slot_1) {
return function() {
err = arguments[0];
return __slot_1.out_data = arguments[1];
};
})(_this),
lineno: 137
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
return cb(err);
};
})(this));
};
Cmd.prototype.write_output = function(cb) {
var d, e, err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
if ((e = this.get_encoding(this.argv.e)) != null) {
d = this.out_data.toString(e);
e = "utf8";
} else {
d = this.out_data;
e = null;
}
(function(_this) {
return (function(__iced_k) {
if (_this.argv.o) {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.write_output"
});
fs.writeFile(_this.argv.o, d, e, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
return err = arguments[0];
};
})(),
lineno: 150
}));
__iced_deferrals._fulfill();
})(__iced_k);
} else {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.write_output"
});
process.stdout.write(d, e, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
return err = arguments[0];
};
})(),
lineno: 152
}));
__iced_deferrals._fulfill();
})(__iced_k);
}
});
})(this)((function(_this) {
return function() {
return cb(err);
};
})(this));
};
Cmd.prototype.run = function(cb) {
var esc, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
esc = make_esc(cb, "Cmd::run");
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.run"
});
_this.parse_args(esc(__iced_deferrals.defer({
lineno: 159
})));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
(function(__iced_k) {
if (_this.argv.h) {
return __iced_k(usage());
} else {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.run"
});
_this.get_key(esc(__iced_deferrals.defer({
lineno: 163
})));
__iced_deferrals._fulfill();
})(function() {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.run"
});
_this.get_input(esc(__iced_deferrals.defer({
lineno: 164
})));
__iced_deferrals._fulfill();
})(function() {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.run"
});
_this.do_cmd(esc(__iced_deferrals.defer({
lineno: 165
})));
__iced_deferrals._fulfill();
})(function() {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced",
funcname: "Cmd.run"
});
_this.write_output(esc(__iced_deferrals.defer({
lineno: 166
})));
__iced_deferrals._fulfill();
})(__iced_k);
});
});
});
}
})(function() {
return cb(null);
});
};
})(this));
};
return Cmd;
})();
exports.main = main = function() {
var err, rc, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/Users/max/src/3s/src/cmd.iced"
});
(new Cmd).run(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
return err = arguments[0];
};
})(),
lineno: 174
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
if (typeof err !== "undefined" && err !== null) {
console.error(err.message);
rc = -2;
} else {
rc = 0;
}
return process.exit(rc);
};
})(this));
};
}).call(this);