UNPKG

redacted-cli

Version:
129 lines (128 loc) 5.81 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const feistel_cipher_1 = require("feistel-cipher"); const figlet_1 = __importDefault(require("figlet")); const fs_1 = require("fs"); const readline = __importStar(require("readline")); const redacted_ts_1 = require("redacted-ts"); const DEFAULT_KEY = 'd51e1d9a9b12cd88a1d232c1b8730a05c8a65d9706f30cdb8e08b9ed4c7b16a0'; console.log(figlet_1.default.textSync('Redacted')); // Define CLI const program = new commander_1.Command(); // TODO Make it programmatic const version = process.env.npm_package_version || '1.0.12'; // eslint-disable-line @typescript-eslint/strict-boolean-expressions program .version(version) .description('A TypeScript-based CLI to redacting classified documents') .option('-b, --both', 'add to use both dictionary and tag') .option('-d, --dictionary <value>', 'the optional path to the dictionary of words to redact') .option('-H, --hash <value>', 'the hash engine for the round function (default "sha-256")') .option('-i, --input <value>', 'the path to the document to be redacted') .option('-k, --key <value>', 'the optional key for the FPE scheme (leave it empty to use default)') .option('-o, --output <value>', 'the name of the output file') .option('-r, --rounds <value>', 'the number of rounds for the Feistel cipher (default 10)') .option('-t, --tag <value>', 'the optional tag that prefixes words to redact (default "~")') .option('-x, --expand', 'add to expand a redacted document') .parse(process.argv); // Parse options const options = program.opts(); const both = options.both === true; const dictionary = options.dictionary !== undefined ? (0, redacted_ts_1.stringToDictionary)((0, fs_1.readFileSync)(options.dictionary, 'utf-8')) : ''; const hashEngine = options.hash !== undefined && (0, feistel_cipher_1.isAvailableEngine)(options.hash) ? options.hash : feistel_cipher_1.SHA_256; const input = options.input !== undefined ? options.input : ''; const key = options.key !== undefined ? options.key : DEFAULT_KEY; const output = options.output !== undefined ? options.output : ''; const rounds = options.rounds !== undefined ? parseInt(options.rounds) : 10; // eslint-disable-line @typescript-eslint/no-unsafe-argument let tag = options.tag !== undefined && options.tag.length === 1 ? options.tag : ''; const expand = options.expand === true; // Check params if (input === '' || output === '') { console.error('\x1b[31m%s\x1b[0m', 'Invalid input and/or output paths'); console.log('==='); program.outputHelp(); } else if (both && dictionary === '') { console.error('\x1b[31m%s\x1b[0m', 'Missing either dictionary path or tag'); console.log('==='); program.outputHelp(); } else if ((0, fs_1.existsSync)(output)) { console.error('\x1b[31m%s\x1b[0m', 'Output file already exists!'); console.log('==='); program.outputHelp(); } else { // Prepare cipher const cipher = new feistel_cipher_1.FPECipher(hashEngine, key, rounds); // Instantiate redactor if (both && tag === '') { tag = redacted_ts_1.DEFAULT_TAG; } const redactor = both ? (0, redacted_ts_1.Redactor)(dictionary, tag, cipher, both) : dictionary !== '' ? (0, redacted_ts_1.RedactorWithDictionary)(dictionary, cipher) : tag !== '' ? (0, redacted_ts_1.RedactorWithTag)(tag, cipher) : (0, redacted_ts_1.DefaultRedactor)(cipher); // Process redaction or expansion if (expand) { console.log('Expanding and cleaning document...'); const lineReader = readline.createInterface({ input: (0, fs_1.createReadStream)(input), terminal: false }); lineReader.on('line', (line) => { (0, fs_1.appendFileSync)(output, redactor.clean(redactor.expand(line))); }); } else { console.log('Redacting document...'); const lineReader = readline.createInterface({ input: (0, fs_1.createReadStream)(input), terminal: false }); lineReader.on('line', (line) => { (0, fs_1.appendFileSync)(output, redactor.redact(line)); }); } console.log('Done!'); }