Allow providing paths to ring file, add -r arg to print regardless of
file type
This commit is contained in:
@@ -10,8 +10,8 @@ an encryption tool!
|
||||
|
||||
## usage
|
||||
```sh
|
||||
ec-pass <input file> -p <password> [-o <out>]
|
||||
dec-pass <input file> -p <password> [-o <out>]
|
||||
ec-key <input file> [-o <out>]
|
||||
dec-key <input file> [-o <out>]
|
||||
```
|
||||
ec-pass <input file> -p <password> [-o <out>] # generates a password-protected velvet file
|
||||
dec-pass <input file> -p <password> [-o <out>] # decrypts password-protected velvet files
|
||||
ec-key <input file> [-o <out>] [-p <key path>] # generates a 32-byte ring and a velvet file
|
||||
dec-key <input file> [-o <out>] [-p <key path>] # decrypts a velvet file using a ring file. assumes that the ring file shares the same name as the input file, unless a key is supplied with -p
|
||||
```
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
@@ -5,6 +7,7 @@
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include "velvet_ring.h"
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
bool is_ascii(const std::string& str) {
|
||||
@@ -56,13 +59,18 @@ std::string get_opt(int argc, char* argv[], const std::string opt) {
|
||||
|
||||
|
||||
std::string help = R"(love is a gentle thing(lx) by maki
|
||||
v1.0
|
||||
v1.1
|
||||
|
||||
usage:
|
||||
ec-pass <input file> -p <password> [-o <out>]
|
||||
dec-pass <input file> -p <password> [-o <out>]
|
||||
ec-key <input file> [-o <out>]
|
||||
dec-key <input file> [-o <out>])";
|
||||
ec-pass <input file> -p <password> [-o <out>] # generates a password-protected velvet file
|
||||
dec-pass <input file> -p <password> [-o <out>] # decrypts password-protected velvet files
|
||||
ec-key <input file> [-o <out>] [-p <key path>] # generates a 32-byte ring and a velvet file
|
||||
dec-key <input file> [-o <out>] [-p <key path>] # decrypts a velvet file using a ring file. assumes that the ring file shares the same name as the input file, unless a key is supplied with -p
|
||||
|
||||
options:
|
||||
-p provide a password or ring file
|
||||
-o set the destination of processed files
|
||||
-r print content if non-ascii file)";
|
||||
|
||||
void usage() {
|
||||
std::cout << help << "\n";
|
||||
@@ -79,8 +87,30 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
std::string mode = argv[1];
|
||||
std::string in = argv[2];
|
||||
std::string out = get_opt(argc, argv, "-o");
|
||||
std::string pass = get_opt(argc, argv, "-p");
|
||||
std::string out = "";
|
||||
std::string pass = "";
|
||||
bool print_raw = false;
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "ro:p:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'r':
|
||||
print_raw = true;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
out = optarg;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
pass = optarg;
|
||||
break;
|
||||
|
||||
default:
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
ByteVec input = read_file(in);
|
||||
@@ -89,6 +119,8 @@ int main(int argc, char* argv[]) {
|
||||
if (mode == "ec-key") {
|
||||
if (out.empty()) out = enc_out(in);
|
||||
std::string ring = ring_name(out);
|
||||
if (!pass.empty())
|
||||
ring = pass;
|
||||
|
||||
ByteVec key = rb(32);
|
||||
ByteVec enc = ec_wky(
|
||||
@@ -106,6 +138,12 @@ int main(int argc, char* argv[]) {
|
||||
else if (mode == "dec-key") {
|
||||
if (out.empty()) out = dec_out(in);
|
||||
std::string ring = ring_name(in);
|
||||
if (!std::filesystem::exists(ring)) {
|
||||
if (!pass.empty())
|
||||
ring = pass;
|
||||
else
|
||||
throw std::runtime_error("provide a ring file!");
|
||||
}
|
||||
|
||||
ByteVec key = read_file(ring);
|
||||
if (key.size() != 32)
|
||||
@@ -114,7 +152,7 @@ int main(int argc, char* argv[]) {
|
||||
std::string dec = dec_wky(input, key);
|
||||
write_file(out, ByteVec(dec.begin(), dec.end()));
|
||||
|
||||
if (is_ascii(dec))
|
||||
if (is_ascii(dec) || print_raw)
|
||||
std::cout << dec;
|
||||
|
||||
//std::cout << "Decrypted " << out << "\n";
|
||||
@@ -145,7 +183,7 @@ int main(int argc, char* argv[]) {
|
||||
std::string dec = dec_p(input, pass);
|
||||
write_file(out, ByteVec(dec.begin(), dec.end()));
|
||||
|
||||
if (is_ascii(dec)) {
|
||||
if (is_ascii(dec) || print_raw) {
|
||||
std::cout << dec;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user