commit 2da2ae017df2137cb6a745dd2e4c5136a2a72213 Author: AurĂ©lie Delhaie Date: Sat Oct 2 19:26:41 2021 +0200 bcrypt utils command line cli diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1ba8796 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5535d34 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,110 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + +[[package]] +name = "bcrypt" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f691e63585950d8c1c43644d11bab9073e40f5060dd2822734ae7c3dc69a3a80" +dependencies = [ + "base64", + "blowfish", + "getrandom", +] + +[[package]] +name = "bcrypt-utils" +version = "1.0.0" +dependencies = [ + "bcrypt", +] + +[[package]] +name = "blowfish" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe3ff3fc1de48c1ac2e3341c4df38b0d1bfb8fdf04632a187c8b75aaa319a7ab" +dependencies = [ + "byteorder", + "cipher", + "opaque-debug", +] + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cipher" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7" +dependencies = [ + "generic-array", +] + +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "typenum" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec" + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4994923 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "bcrypt-utils" +version = "1.0.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bcrypt = "0.10" + +[profile.release] +opt-level = 3 \ No newline at end of file diff --git a/bcrypt-utils.iml b/bcrypt-utils.iml new file mode 100644 index 0000000..2fecef3 --- /dev/null +++ b/bcrypt-utils.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3d0b5db --- /dev/null +++ b/src/main.rs @@ -0,0 +1,91 @@ +extern crate bcrypt; + +use std::env; +use bcrypt::{DEFAULT_COST, hash, verify}; + +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"); + } + } +} + +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()); + } + } 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" }); + } + } 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(); + } + } + 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