mirror of
https://github.com/arkorty/rustcm-cli.git
synced 2026-03-18 00:57:17 +00:00
Update src/main.rs
This commit is contained in:
75
src/main.rs
75
src/main.rs
@@ -26,10 +26,16 @@ pub fn get_password(prompt: &str) -> String {
|
|||||||
|
|
||||||
pub fn encrypt(plaintext: String, secret_key: orion::kdf::SecretKey) -> Vec<u8> {
|
pub fn encrypt(plaintext: String, secret_key: orion::kdf::SecretKey) -> Vec<u8> {
|
||||||
let plaintext = plaintext.into_bytes();
|
let plaintext = plaintext.into_bytes();
|
||||||
into_match(
|
match aead::seal(&secret_key, &plaintext) {
|
||||||
aead::seal(&secret_key, &plaintext),
|
Ok(temp) => {
|
||||||
"Error: Could not encrypt the data",
|
println!("Success: Data was encrypted");
|
||||||
)
|
temp
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
eprintln!("Error: Could not encrypt the data");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_secret_key(salt_bytes: [u8; 32], password: String) -> orion::kdf::SecretKey {
|
pub fn get_secret_key(salt_bytes: [u8; 32], password: String) -> orion::kdf::SecretKey {
|
||||||
@@ -53,7 +59,7 @@ pub fn get_salt_bytes() -> [u8; 32] {
|
|||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Error: Could not generate the random bytes for the salt");
|
eprintln!("Error: Could not generate the random bytes for the salt");
|
||||||
exit(0)
|
exit(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -131,10 +137,16 @@ pub fn read_cipher(path: String) -> (Vec<u8>, Vec<u8>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt(ciphertext: Vec<u8>, secret_key: orion::kdf::SecretKey) -> String {
|
pub fn decrypt(ciphertext: Vec<u8>, secret_key: orion::kdf::SecretKey) -> String {
|
||||||
let plaintext = into_match(
|
let plaintext = match aead::open(&secret_key, &ciphertext) {
|
||||||
aead::open(&secret_key, &ciphertext),
|
Ok(temp) => {
|
||||||
"Error: Failed to decrypt the file, please check the password",
|
println!("Success: Data was decrypted");
|
||||||
);
|
temp
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
eprintln!("Error: Failed to decrypt the file, please check the password");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
String::from_utf8(plaintext).expect("Error: Could not convert to String")
|
String::from_utf8(plaintext).expect("Error: Could not convert to String")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,13 +172,19 @@ fn main() {
|
|||||||
while args.next() != None {
|
while args.next() != None {
|
||||||
let arg_str: String = match args.next() {
|
let arg_str: String = match args.next() {
|
||||||
Some(temp) => temp,
|
Some(temp) => temp,
|
||||||
None => {
|
None => exit(0),
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match arg_str.as_str() {
|
match arg_str.as_str() {
|
||||||
"--help" | "-h" => {
|
"--help" | "-h" => {
|
||||||
|
match args.next() {
|
||||||
|
Some(_) => {
|
||||||
|
eprintln!("Error: Too many arguments");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
};
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{PROGRAM_NAME} {PROGRAM_VERSION}
|
"{PROGRAM_NAME} {PROGRAM_VERSION}
|
||||||
Rust Simple Text Cipher Machine.
|
Rust Simple Text Cipher Machine.
|
||||||
@@ -186,10 +204,19 @@ COMMAND:
|
|||||||
|
|
||||||
-d, --decrypt <input-path> <output-path>
|
-d, --decrypt <input-path> <output-path>
|
||||||
Runs the program in decryption mode."
|
Runs the program in decryption mode."
|
||||||
)
|
);
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
"--version" | "-v" => {
|
"--version" | "-v" => {
|
||||||
|
match args.next() {
|
||||||
|
Some(_) => {
|
||||||
|
eprintln!("Error: Too many arguments");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
};
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"rustcm-cli (0.1.0-alpha)
|
"rustcm-cli (0.1.0-alpha)
|
||||||
Copyright (C) 2023 Arkaprabha Chakraborty
|
Copyright (C) 2023 Arkaprabha Chakraborty
|
||||||
@@ -199,6 +226,7 @@ There is NO WARRANTY, to the extent permitted by law.
|
|||||||
|
|
||||||
Written by Arkaprabha Chakraborty"
|
Written by Arkaprabha Chakraborty"
|
||||||
);
|
);
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
"--encrypt" | "-e" => {
|
"--encrypt" | "-e" => {
|
||||||
let path: String = match args.next() {
|
let path: String = match args.next() {
|
||||||
@@ -217,6 +245,14 @@ Written by Arkaprabha Chakraborty"
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match args.next() {
|
||||||
|
Some(_) => {
|
||||||
|
eprintln!("Error: Too many arguments");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
};
|
||||||
|
|
||||||
let plaintext: String = read_plain(path);
|
let plaintext: String = read_plain(path);
|
||||||
let salt_bytes = get_salt_bytes();
|
let salt_bytes = get_salt_bytes();
|
||||||
let password: String = get_password("Password: ");
|
let password: String = get_password("Password: ");
|
||||||
@@ -225,7 +261,7 @@ Written by Arkaprabha Chakraborty"
|
|||||||
|
|
||||||
write_cipher(output, salt_bytes, ciphertext);
|
write_cipher(output, salt_bytes, ciphertext);
|
||||||
|
|
||||||
println!("File encrypted successfully")
|
exit(0);
|
||||||
}
|
}
|
||||||
"--decrypt" | "-d" => {
|
"--decrypt" | "-d" => {
|
||||||
let path: String = match args.next() {
|
let path: String = match args.next() {
|
||||||
@@ -244,6 +280,14 @@ Written by Arkaprabha Chakraborty"
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match args.next() {
|
||||||
|
Some(_) => {
|
||||||
|
eprintln!("Error: Too many arguments");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
};
|
||||||
|
|
||||||
let (salt_bytes, ciphertext) = read_cipher(path);
|
let (salt_bytes, ciphertext) = read_cipher(path);
|
||||||
let salt_bytes: [u8; 32] = to_array(salt_bytes);
|
let salt_bytes: [u8; 32] = to_array(salt_bytes);
|
||||||
let password: String = get_password("Password: ");
|
let password: String = get_password("Password: ");
|
||||||
@@ -252,10 +296,11 @@ Written by Arkaprabha Chakraborty"
|
|||||||
|
|
||||||
write_plain(output, plaintext);
|
write_plain(output, plaintext);
|
||||||
|
|
||||||
println!("File decryption was successful")
|
exit(0);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Error: Unrecognized argument");
|
eprintln!("Error: Unrecognized argument");
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user