Skip to content

Commit

Permalink
feat: make i6 pack encryption optional and take the password with stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
kruserr committed Oct 10, 2024
1 parent 78d8bbd commit 6c1f369
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 35 deletions.
114 changes: 101 additions & 13 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions i6-pack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ rand = "0.8"
hmac = "0.12"
uuid = {version = "1", features = ["v4"]}
argon2 = "0.5"
rpassword = "7"
56 changes: 46 additions & 10 deletions i6-pack/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
use std::io::Write;

use crate::compression;
use crate::encryption;
use crate::utils;

pub fn run(action: &str, target: &str, password: &str) -> std::io::Result<()> {
pub fn run(action: &str, target: &str, encrypt: bool) -> std::io::Result<()> {
let password = &if encrypt {
print!("Enter password: ");
std::io::stdout().flush().expect("Failed to flush stdout");
let password1 = rpassword::read_password().expect("Failed to read password");

if (action == "pack") {
print!("Confirm password: ");
std::io::stdout().flush().expect("Failed to flush stdout");
let password2 = rpassword::read_password().expect("Failed to read password");

if password1 != password2 {
eprintln!("Passwords do not match.");
std::process::exit(1);
}
}

password1
} else {
"".to_owned()
};

// Validate and sanitize the target path
let target_path = utils::validate_path(target)
.or_else(|_| utils::sanitize_output_path(target))
Expand All @@ -17,16 +40,26 @@ pub fn run(action: &str, target: &str, password: &str) -> std::io::Result<()> {
match action {
"pack" => {
compression::create_tar_archive(target_path.to_str().unwrap(), tar_file)?;
compression::compress_tar_file(tar_file, compressed_file)?;
encryption::encrypt_file(compressed_file, encrypted_file, password)?;

if (encrypt) {
compression::compress_tar_file(tar_file, compressed_file)?;
encryption::encrypt_file(compressed_file, encrypted_file, password)?;
} else {
compression::compress_tar_file(tar_file, encrypted_file)?;
}
}
"unpack" => {
encryption::decrypt_file(
target_path.to_str().unwrap(),
compressed_file,
password,
)?;
compression::decompress_file(compressed_file, tar_file)?;
if (encrypt) {
encryption::decrypt_file(
target_path.to_str().unwrap(),
compressed_file,
password,
)?;
compression::decompress_file(compressed_file, tar_file)?;
} else {
compression::decompress_file(target_path.to_str().unwrap(), tar_file)?;
}

compression::extract_tar_archive(
tar_file,
&utils::remove_extension(target_path.to_str().unwrap(), ".i6p"),
Expand All @@ -40,7 +73,10 @@ pub fn run(action: &str, target: &str, password: &str) -> std::io::Result<()> {

// Clean up temporary files
std::fs::remove_file(tar_file)?;
std::fs::remove_file(compressed_file)?;

if (encrypt) {
std::fs::remove_file(compressed_file)?;
}

Ok(())
}
13 changes: 7 additions & 6 deletions i6-pack/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ fn main() -> io::Result<()> {
.index(2),
)
.arg(
Arg::new("password")
.help("Password for encryption/decryption")
.required(true)
.index(3),
Arg::new("encrypt")
.help("Flag to indicate encryption/decryption")
.short('e')
.long("encrypt")
.action(clap::ArgAction::SetTrue),
)
.get_matches();

let action = matches.get_one::<String>("action").unwrap();
let target = matches.get_one::<String>("target").unwrap();
let password = matches.get_one::<String>("password").unwrap();
let encrypt = matches.get_flag("encrypt");

cli::run(action, target, password)
cli::run(action, target, encrypt)
}
13 changes: 7 additions & 6 deletions i6/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
.index(2),
)
.arg(
Arg::with_name("password")
.help("Password for encryption/decryption")
.required(true)
.index(3),
Arg::with_name("encrypt")
.help("Flag to indicate encryption/decryption")
.short('e')
.long("encrypt")
.takes_value(false),
),
)
.get_matches();
Expand Down Expand Up @@ -155,9 +156,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(matches) = matches.subcommand_matches(pack_id) {
let action = matches.value_of("action").unwrap();
let target = matches.value_of("target").unwrap();
let password = matches.value_of("password").unwrap();
let encrypt = matches.is_present("encrypt");

i6_pack::cli::run(action, target, password)?;
i6_pack::cli::run(action, target, encrypt)?;
}

Ok(())
Expand Down

0 comments on commit 6c1f369

Please sign in to comment.