bcrypt utils command line cli

This commit is contained in:
Aurélie Delhaie
2021-10-02 19:26:41 +02:00
commit 2da2ae017d
10 changed files with 264 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

8
.idea/.gitignore generated vendored Normal file
View File

@@ -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

6
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/bcrypt-utils.iml" filepath="$PROJECT_DIR$/bcrypt-utils.iml" />
</modules>
</component>
</project>

10
.idea/runConfigurations.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

110
Cargo.lock generated Normal file
View File

@@ -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"

12
Cargo.toml Normal file
View File

@@ -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

12
bcrypt-utils.iml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUST_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

91
src/main.rs Normal file
View File

@@ -0,0 +1,91 @@
extern crate bcrypt;
use std::env;
use bcrypt::{DEFAULT_COST, hash, verify};
fn main() {
let args: Vec<String> = 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 <TEXT> Hash the text");
println!(" verify, v <TEXT> <HASH> Check if the hash is valid");
println!(" help, ? Show this screen");
println!();
println!("OPTION:");
println!(" --cost, -c <COST> How many times to run the hash command before print result, default : {}", DEFAULT_COST);
}
fn hash_subcommand() {
let args: Vec<String> = 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<String> = 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::<u32>();
if parsed.is_ok() {
return parsed.unwrap();
}
}
DEFAULT_COST
}
fn get_params(param: String, short: String) -> Option<String> {
let args: Vec<String> = env::args().collect();
for (i, arg) in args.iter().enumerate() {
if (arg == &param || arg == &short) && args.len() > (i + 1) {
return Option::from(args[i + 1].clone());
}
}
None
}