Skip to content

Commit

Permalink
feat: added verbose mode option
Browse files Browse the repository at this point in the history
  • Loading branch information
FajarKim committed Feb 13, 2024
1 parent 708f557 commit 7cdd738
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 18 deletions.
76 changes: 75 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require("fs");
const path = require("path");
const https = require("https");
const checknet = require("@barudakrosul/internet-available");
const millify = require("millify");
const { program } = require("commander");
const gcrypt = require("../index");
const printf = require("../lib/printf");
Expand All @@ -19,11 +20,12 @@ try {
.description("Go-crypt is simple encryption and decryption using PBKDF2, zlib, and AES-256-GCM.")
.option("-f, --file <file_name>", "input file name for encryption or decryption")
.option("-o, --output <out_name>", "save result to out name")
.option("-v, --verbose", "verbose mode")
.option("-d, --decrypt", "starting decryption")
.option("-p, --passkey <pass>", "enter passphrase key (default: '12345678')")
.option("-c, --stdout", "write output to terminal")
.action((options) => {
let { file, output, decrypt, passkey, stdout } = options;
let { file, output, verbose, decrypt, passkey, stdout } = options;

(async () => {
if (await checknet.checkWithAxios()) {
Expand All @@ -43,8 +45,26 @@ try {

if (decrypt) {
if (file) {
const firstTimeout = new Date();
const data = fs.readFileSync(file);
const decrypted = gcrypt.decrypt(data, passkey);
const lastTimeout = new Date();
const originalBytes = Buffer.from(data, "utf-8").length;
const outputBytes = Buffer.from(decrypted, "utf-8").length;
const timeout = millify.millify((lastTimeout - firstTimeout) / 1000, { precision: 2 });
let percentDecrypted = ((originalBytes - outputBytes) / (originalBytes + outputBytes)) * 100;

if (percentDecrypted < 0) {
percentDecrypted += 100;
}

if (verbose) {
printf("Buffer size : 8192");
printf(`Original bytes : ${originalBytes} bytes`);
printf(`Output bytes : ${outputBytes} bytes`);
printf(`Timeout : ${timeout} sec`);
printf(`Decrypted : ${millify.millify(percentDecrypted, { precision: 2})}%`);
}

if (stdout) {
process.stdout.write(decrypted);
Expand Down Expand Up @@ -80,7 +100,25 @@ try {
inputText = Buffer.concat([buffer, inputText]);
}

const firstTimeout = new Date();
const decrypted = gcrypt.decrypt(inputText, passkey);
const lastTimeout = new Date();
const originalBytes = Buffer.from(inputText, "utf-8").length;
const outputBytes = Buffer.from(decrypted, "utf-8").length;
const timeout = millify.millify((lastTimeout - firstTimeout) / 1000, { precision: 2 });
let percentDecrypted = ((originalBytes - outputBytes) / (originalBytes + outputBytes)) * 100;

if (percentDecrypted < 0) {
percentDecrypted += 100;
}

if (verbose) {
printf("Buffer size : 8192");
printf(`Original bytes : ${originalBytes} bytes`);
printf(`Output bytes : ${outputBytes} bytes`);
printf(`Timeout : ${timeout} sec`);
printf(`Decrypted : ${millify.millify(percentDecrypted, { precision: 2})}%`);
}

if (! output) {
process.stdout.write(decrypted);
Expand All @@ -95,8 +133,26 @@ try {
}
} else {
if (file) {
const firstTimeout = new Date();
const data = fs.readFileSync(file, "utf-8");
const encrypted = gcrypt.encrypt(data, passkey);
const lastTimeout = new Date();
const originalBytes = Buffer.from(data, "utf-8").length;
const outputBytes = Buffer.from(encrypted, "utf-8").length;
const timeout = millify.millify((lastTimeout - firstTimeout) / 1000, { precision: 2 });
let percentEncrypted = ((originalBytes - outputBytes) / (originalBytes + outputBytes)) * 100;

if (percentEncrypted < 0) {
percentEncrypted += 100;
}

if (verbose) {
printf("Buffer size : 8192");
printf(`Original bytes : ${originalBytes} bytes`);
printf(`Output bytes : ${outputBytes} bytes`);
printf(`Timeout : ${timeout} sec`);
printf(`Encrypted : ${millify.millify(percentEncrypted, { precision: 2})}%`);
}

if (stdout) {
process.stdout.write(encrypted);
Expand Down Expand Up @@ -127,7 +183,25 @@ try {
process.exit(1);
}

const firstTimeout = new Date();
const encrypted = gcrypt.encrypt(inputText, passkey);
const lastTimeout = new Date();
const originalBytes = Buffer.from(inputText, "utf-8").length;
const outputBytes = Buffer.from(encrypted, "utf-8").length;
const timeout = millify.millify((lastTimeout - firstTimeout) / 1000, { precision: 2 });
let percentEncrypted = ((originalBytes - outputBytes) / (originalBytes + outputBytes)) * 100;

if (percentEncrypted < 0) {
percentEncrypted += 100;
}

if (verbose) {
printf("Buffer size : 8192");
printf(`Original bytes : ${originalBytes} bytes`);
printf(`Output bytes : ${outputBytes} bytes`);
printf(`Timeout : ${timeout} sec`);
printf(`Encrypted : ${millify.millify(percentEncrypted, { precision: 2})}%`);
}

if (! output) {
process.stdout.write(encrypted);
Expand Down
69 changes: 53 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"homepage": "https://github.com/BarudakRosul/go-crypt#readme",
"dependencies": {
"@barudakrosul/internet-available": "^0.1.1",
"commander": "^12.0.0"
"commander": "^12.0.0",
"millify": "^6.1.0"
},
"devDependencies": {
"mocha": "^10.2.0"
Expand Down

0 comments on commit 7cdd738

Please sign in to comment.