diff --git a/.idea/modules.xml b/.idea/modules.xml index 1ba8796..1359cd1 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5535d34..c767595 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,28 +2,59 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "base64" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "base64" +version = "0.20.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149ea5dc24cb11513350770afebba32b68e3d2e356f9221351a2a1ee89112a82" + [[package]] name = "bcrypt" version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f691e63585950d8c1c43644d11bab9073e40f5060dd2822734ae7c3dc69a3a80" dependencies = [ - "base64", + "base64 0.13.0", "blowfish", "getrandom", ] [[package]] -name = "bcrypt-utils" -version = "1.0.0" +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "block-buffer" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" dependencies = [ - "bcrypt", + "generic-array", ] [[package]] @@ -59,10 +90,69 @@ dependencies = [ ] [[package]] -name = "generic-array" -version = "0.14.4" +name = "clap" +version = "3.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "lazy_static", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25320346e922cffe59c0bbc5410c8d8784509efb321488971081313cb1e1a33c" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "crypto-common" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" dependencies = [ "typenum", "version_check", @@ -70,9 +160,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.3" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" dependencies = [ "cfg-if", "libc", @@ -80,10 +170,66 @@ dependencies = [ ] [[package]] -name = "libc" -version = "0.2.103" +name = "hash_utils" +version = "1.0.0" +dependencies = [ + "base64 0.20.0-alpha.1", + "bcrypt", + "clap", + "md-5", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "md-5" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "658646b21e0b72f7866c7038ab086d3d5e1cd6271f060fd37defb241949d0582" +dependencies = [ + "digest", +] [[package]] name = "opaque-debug" @@ -92,19 +238,136 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] -name = "typenum" -version = "1.14.0" +name = "os_str_bytes" +version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec" +checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + +[[package]] +name = "unicode-ident" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" [[package]] name = "version_check" -version = "0.9.3" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 4994923..39d0410 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bcrypt-utils" +name = "hash_utils" version = "1.0.0" edition = "2018" @@ -7,6 +7,9 @@ edition = "2018" [dependencies] bcrypt = "0.10" +clap = { version = "3.1.18", features = ["derive"] } +md-5 = "0.10.1" +base64 = "0.20.0-alpha.1" [profile.release] opt-level = 3 \ No newline at end of file diff --git a/bcrypt-utils.iml b/hash_utils.iml similarity index 100% rename from bcrypt-utils.iml rename to hash_utils.iml diff --git a/src/base64_utils.rs b/src/base64_utils.rs new file mode 100644 index 0000000..560d152 --- /dev/null +++ b/src/base64_utils.rs @@ -0,0 +1,37 @@ +use clap::{Subcommand}; +use std::str; + +#[derive(Subcommand)] +pub enum Base64SubCommands { + /// Encode the text in base64 + Encode { + /// The text to encode + text: Option, + }, + + /// Decode the text + Decode { + /// Base64 encoded text + text: Option, + }, +} + +pub fn do_base64_encode(text: String) { + let b64 = base64::encode(text); + println!("{}", b64); +} + +pub fn do_base64_decode(text: String) { + let res = base64::decode(text); + if res.is_err() { + eprintln!("E: {}", res.unwrap_err()); + return; + } + let bytes = res.unwrap(); + let utf8_text = str::from_utf8(&bytes); + if utf8_text.is_err() { + eprintln!("E: {}", utf8_text.unwrap_err()); + return; + } + println!("{}", utf8_text.unwrap()); +} \ No newline at end of file diff --git a/src/bcrypt_utils.rs b/src/bcrypt_utils.rs new file mode 100644 index 0000000..45fa43e --- /dev/null +++ b/src/bcrypt_utils.rs @@ -0,0 +1,42 @@ +extern crate bcrypt; + +use bcrypt::{DEFAULT_COST, hash, verify}; +use clap::{Subcommand}; + +#[derive(Subcommand)] +pub enum BCryptSubCommands { + /// Hash the text and print it + Hash { + /// How many times to run the hash command before print result + #[clap(short, long, default_value_t = DEFAULT_COST)] + cost: u32, + /// The text to hash + text: Option, + }, + + /// Verify the text hashed and print the result + Verify { + /// The hash to verify + hash: Option, + /// The clear text that need to be verified with the hash + text: Option, + }, +} + +pub fn do_bcrypt_hash(text: String, cost: u32) { + let hashed = hash(text, cost); + if hashed.is_err() { + eprintln!("E: {}", hashed.err().unwrap()) + } else { + println!("{}", hashed.unwrap()); + } +} + +pub fn do_bcrypt_verify(hash: String, text: String) { + let valid = verify(text, &hash); + if valid.is_err() { + eprintln!("E: {}", valid.err().unwrap()) + } else { + println!("{} : {}", hash, if valid.unwrap() { "true" } else { "false" }); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3d0b5db..73e4289 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,91 +1,107 @@ -extern crate bcrypt; +mod bcrypt_utils; +mod md5_utils; +mod base64_utils; -use std::env; -use bcrypt::{DEFAULT_COST, hash, verify}; +pub use crate::bcrypt_utils::{do_bcrypt_hash, do_bcrypt_verify, BCryptSubCommands}; +pub use crate::md5_utils::{do_md5_hash, do_md5_verify, MD5SubCommands}; +pub use crate::base64_utils::{do_base64_encode, do_base64_decode, Base64SubCommands}; +use clap::{Parser, Subcommand}; + +#[derive(Parser)] +#[clap(author, version, about, long_about = None)] +struct Cli { + #[clap(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + /// Use the bcrypt algorithm + Bcrypt { + #[clap(subcommand)] + command: Option, + }, + /// Use the md5 algorithm + MD5 { + #[clap(subcommand)] + command: Option, + }, + /// Encode and decode base64 text (I know, it's not a hash but I need it in my tools) + Base64 { + #[clap(subcommand)] + command: Option, + }, +} fn main() { - let args: Vec = env::args().collect(); - if args.len() <= 1 { - help(); - } else { - let cmd = &args[1]; - if cmd == "hash" || cmd == "h" { - hash_subcommand(); - } else if cmd == "verify" || cmd == "v" { - verify_subcommand(); - } else if cmd == "help" || cmd == "?" { - help(); - } else { - eprintln!("E: This subcommand is not valid, type \"bcrypt help\" to show valid subcommands"); - } - } -} + let cmd = Cli::parse(); -fn help() { - println!("BCrypt command utility"); - println!("Github : https://github.com/mojitaurelie"); - println!(); - println!("USAGE:"); - println!(" bcrypt [SUBCOMMAND]"); - println!(); - println!("COMMANDS:"); - println!(" hash, h Hash the text"); - println!(" verify, v Check if the hash is valid"); - println!(" help, ? Show this screen"); - println!(); - println!("OPTION:"); - println!(" --cost, -c How many times to run the hash command before print result, default : {}", DEFAULT_COST); -} - -fn hash_subcommand() { - let args: Vec = env::args().collect(); - if args.len() >= 3 { - let text = &args[2]; - let cost = get_cost(); - let hashed = hash(text, cost); - if hashed.is_err() { - eprintln!("E: {}", hashed.err().unwrap()) - } else { - println!("{}", hashed.unwrap()); + match &cmd.command { + Some(Commands::Bcrypt { command }) => { + match &command { + Some(BCryptSubCommands::Hash { cost, text }) => { + if let Some(value) = text { + do_bcrypt_hash(String::from(value), *cost); + } else { + eprintln!("E: Missing the text value") + } + } + Some(BCryptSubCommands::Verify { hash, text }) => { + if let Some(value) = text { + if let Some(hashed_value) = hash { + do_bcrypt_verify(String::from(hashed_value), String::from(value)); + } else { + eprintln!("E: Missing the hash") + } + } else { + eprintln!("E: Missing the text value") + } + } + None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")} + } } - } else { - eprintln!("E: Some arguments are missing, type \"bcrypt help\""); - } -} - -fn verify_subcommand() { - let args: Vec = env::args().collect(); - if args.len() >= 4 { - let text = &args[2]; - let hash_text = &args[3]; - let valid = verify(text, &hash_text); - if valid.is_err() { - eprintln!("E: {}", valid.err().unwrap()) - } else { - println!("{} : {}", hash_text, if valid.unwrap() { "TRUE" } else { "FALSE" }); + Some(Commands::MD5 { command }) => { + match &command { + Some(MD5SubCommands::Hash { text }) => { + if let Some(value) = text { + do_md5_hash(String::from(value)); + } else { + eprintln!("E: Missing the text value") + } + } + Some(MD5SubCommands::Verify { hash, text }) => { + if let Some(value) = text { + if let Some(hashed_value) = hash { + do_md5_verify(String::from(hashed_value), String::from(value)); + } else { + eprintln!("E: Missing the hash") + } + } else { + eprintln!("E: Missing the text value") + } + } + None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")} + } } - } else { - eprintln!("E: Some arguments are missing, type \"bcrypt help\""); - } -} - -fn get_cost() -> u32 { - let cost = get_params("--cost".to_string(), "cost".to_string()); - if cost.is_some() { - let parsed = cost.unwrap().parse::(); - if parsed.is_ok() { - return parsed.unwrap(); + Some(Commands::Base64 { command }) => { + match &command { + Some(Base64SubCommands::Encode { text }) => { + if let Some(value) = text { + do_base64_encode(String::from(value)); + } else { + eprintln!("E: Missing the text value") + } + } + Some(Base64SubCommands::Decode { text }) => { + if let Some(value) = text { + do_base64_decode(String::from(value)); + } else { + eprintln!("E: Missing the encoded value") + } + } + None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")} + } } + None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")} } - DEFAULT_COST -} - -fn get_params(param: String, short: String) -> Option { - let args: Vec = env::args().collect(); - for (i, arg) in args.iter().enumerate() { - if (arg == ¶m || arg == &short) && args.len() > (i + 1) { - return Option::from(args[i + 1].clone()); - } - } - None } \ No newline at end of file diff --git a/src/md5_utils.rs b/src/md5_utils.rs new file mode 100644 index 0000000..112d2a0 --- /dev/null +++ b/src/md5_utils.rs @@ -0,0 +1,33 @@ +use md5::{Md5, Digest}; +use clap::{Subcommand}; + +#[derive(Subcommand)] +pub enum MD5SubCommands { + /// Hash the text and print it + Hash { + /// The text to hash + text: Option, + }, + + /// Verify the text hashed and print the result + Verify { + /// The hash to verify + hash: Option, + /// The clear text that need to be verified with the hash + text: Option, + }, +} + +pub fn do_md5_hash(text: String) { + let mut hasher = Md5::new(); + hasher.update(text); + let result = hasher.finalize(); + println!("{:x}", result) +} + +pub fn do_md5_verify(hash: String, text: String) { + let mut hasher = Md5::new(); + hasher.update(text); + let result = hasher.finalize(); + println!("{} : {}", hash, if format!("{:x}", result) == hash { "true" } else { "false" }); +} \ No newline at end of file