From da2475aded3aa66e0d64e594c763ea16717c6de0 Mon Sep 17 00:00:00 2001 From: rrryfoo Date: Tue, 24 Mar 2026 22:56:17 +0800 Subject: [PATCH] Allow providing paths to ring file, add -r arg to print regardless of file type --- README.md | 10 +++++----- main.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4f25e13..7f881ce 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ an encryption tool! ## usage ```sh -ec-pass -p [-o ] -dec-pass -p [-o ] -ec-key [-o ] -dec-key [-o ] -``` +ec-pass -p [-o ] # generates a password-protected velvet file +dec-pass -p [-o ] # decrypts password-protected velvet files +ec-key [-o ] [-p ] # generates a 32-byte ring and a velvet file +dec-key [-o ] [-p ] # 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 +``` \ No newline at end of file diff --git a/main.cpp b/main.cpp index 2591477..19d253e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -5,6 +7,7 @@ #include #include #include "velvet_ring.h" +#include 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 -p [-o ] - dec-pass -p [-o ] - ec-key [-o ] - dec-key [-o ])"; + ec-pass -p [-o ] # generates a password-protected velvet file + dec-pass -p [-o ] # decrypts password-protected velvet files + ec-key [-o ] [-p ] # generates a 32-byte ring and a velvet file + dec-key [-o ] [-p ] # 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; }