This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[⭐] Added SHA512 and improved efficiency with rayon
significant improvements to the hasher program. We’ve added SHA512 to the list of hash algorithms, alongside the existing SHA256 and MD5. To boost efficiency, we’ve also incorporated the use of a thread pool via the rayon crate for concurrent computation of hashes. This update enhances the program’s performance and capabilities.
- Loading branch information
Showing
4 changed files
with
92 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ edition = "2021" | |
[dependencies] | ||
rust-crypto = "0.2.36" | ||
console = "0.15.5" | ||
rayon = "1.10.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,42 @@ | ||
# hasher | ||
This is a simple Rust program that calculates the SHA256 and MD5 hashes of a given file. The program uses multithreading to compute both hashes concurrently and outputs the result along with the count of each hash type. | ||
|
||
|
||
the first code reads the file content, then spawns two threads to calculate the SHA256 and MD5 hashes. The results are stored in a shared hash map, which is then printed to the console. | ||
This is a Rust program that calculates the SHA256, MD5, and SHA512 hashes of a given file. The program uses a thread pool to compute all hashes concurrently and outputs the result. | ||
|
||
The program reads the file content, then uses a thread pool to calculate the SHA256, MD5, and SHA512 hashes. The results are then printed to the console. | ||
|
||
# Dependencies | ||
|
||
The following external crates are used in this program: | ||
|
||
crypto: Provides various cryptographic algorithms, including SHA256 and MD5. | ||
console: Offers utility functions to interact with the terminal. | ||
- `crypto`: Provides various cryptographic algorithms, including SHA256, MD5, and SHA512. | ||
- `console`: Offers utility functions to interact with the terminal. | ||
- `rayon`: A data parallelism library for Rust. | ||
|
||
Add the following lines to your Cargo.toml file to include these dependencies: | ||
|
||
[dependencies] | ||
crypto = "0.2.36" | ||
console = "0.15.5" | ||
```toml | ||
[dependencies] | ||
crypto = "0.2.36" | ||
console = "0.15.5" | ||
rayon = "1.5.1" | ||
``` | ||
|
||
# Usage | ||
## Method 1: Drag and Drop | ||
|
||
Compile the Rust code into an executable by running cargo build --release. This will generate a hasher.exe file in the target/release directory. | ||
Locate the hasher.exe file in the target/release directory. | ||
Drag and drop the file you want to hash onto the hasher.exe file. The application will open up in a command prompt or PowerShell window, showing the calculated hashes for the dragged file. | ||
Compile the Rust code into an executable by running `cargo build --release`. This will generate a `hasher.exe` file in the target/release directory. | ||
Locate the `hasher.exe` file in the `target/release` directory. | ||
Drag and drop the file you want to hash onto the `hasher.exe` file. The application will open up in a command prompt or PowerShell window, showing the calculated hashes for the dragged file. | ||
|
||
## Method 2: Command Line | ||
|
||
Compile the Rust code into an executable by running cargo build --release. This will generate a hasher.exe file in the target/release directory. | ||
Compile the Rust code into an executable by running `cargo build --release`. This will generate a `hasher.exe` file in the `target/release` directory. | ||
Open a command prompt (CMD) or PowerShell window. | ||
Navigate to the directory containing the hasher.exe file using the cd command. | ||
Run the hasher application with the file path as an argument, like this: hasher.exe <file_location>. Replace <file_location> with the path to the file you want to hash. | ||
Run the hasher application with the file path as an argument, like this: `hasher.exe <file_location>`. Replace `<file_location>` with the path to the file you want to hash. | ||
|
||
For example, to grab a hash of the file named example.txt located in the same directory as hasher.exe, you would run the following command: | ||
For example, to grab a hash of the file named `example.txt` located in the same directory as `hasher.exe`, you would run the following command: | ||
|
||
hasher.exe example.txt | ||
The application will then calculate the SHA256 and MD5 hashes of the specified file and display the results in the command prompt or PowerShell window. | ||
|
||
|
||
The application will then calculate the SHA256, MD5, and SHA512 hashes of the specified file and display the results in the command prompt or PowerShell window. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,40 @@ | ||
// Import needed modules | ||
use std::{env, fs}; | ||
use std::io::{self, Write, Read}; | ||
use crypto::digest::Digest; | ||
use crypto::sha2::Sha256; | ||
use crypto::sha2::{Sha256, Sha512}; | ||
use crypto::md5::Md5; | ||
|
||
use std::collections::HashMap; | ||
use std::thread; | ||
use std::sync::{Arc, Mutex}; | ||
use console::Term; | ||
use std::io; | ||
use rayon::prelude::*; | ||
|
||
fn calculate_hash(file_content: &[u8], mut hasher: Box<dyn Digest + Send>, hash_type: String) -> String { | ||
hasher.input(file_content); | ||
let hash = hasher.result_str(); | ||
format!("{}: {}", hash_type, hash) | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
// Initialize terminal | ||
let term = Term::stdout(); | ||
term.set_title("Hasher"); | ||
|
||
// Get command line arguments and check if a file path is provided | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() < 2 { | ||
panic!("Please provide a file path as an argument."); | ||
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Please provide a file path as an argument.")); | ||
} | ||
let file_path = &args[1]; | ||
|
||
// Read the file content | ||
let file_content = fs::read(file_path)?; | ||
|
||
// Initialize a shared hash map to store the hashes and their counts | ||
let hash_map: Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new())); | ||
|
||
// Spawn a thread to calculate the SHA256 hash | ||
let sha256_content = file_content.clone(); | ||
let sha256_hash_map = Arc::clone(&hash_map); | ||
let sha256_thread = thread::spawn(move || { | ||
let mut sha256 = Sha256::new(); | ||
sha256.input(&sha256_content); | ||
let sha256_hash = sha256.result_str(); | ||
let mut sha256_hash_map = sha256_hash_map.lock().unwrap(); | ||
sha256_hash_map.entry(sha256_hash).and_modify(|count| *count += 1).or_insert(1); | ||
}); | ||
let hashes: Vec<String> = vec![ | ||
("SHA256", Box::new(Sha256::new()) as Box<dyn Digest + Send>), | ||
("MD5", Box::new(Md5::new()) as Box<dyn Digest + Send>), | ||
("SHA512", Box::new(Sha512::new()) as Box<dyn Digest + Send>), | ||
].into_par_iter() // Use into_par_iter instead of par_iter | ||
.map(|(hash_type, hasher)| calculate_hash(&file_content, hasher, hash_type.to_string())) | ||
.collect(); | ||
|
||
// Spawn a thread to calculate the MD5 hash | ||
let md5_content = file_content.clone(); | ||
let md5_hash_map = Arc::clone(&hash_map); | ||
let md5_thread = thread::spawn(move || { | ||
let mut md5 = Md5::new(); | ||
md5.input(&md5_content); | ||
let md5_hash = md5.result_str(); | ||
let mut md5_hash_map = md5_hash_map.lock().unwrap(); | ||
md5_hash_map.entry(md5_hash).and_modify(|count| *count += 1).or_insert(1); | ||
}); | ||
|
||
// Wait for both threads to finish | ||
sha256_thread.join().unwrap(); | ||
md5_thread.join().unwrap(); | ||
|
||
// Print the hashes and their counts | ||
let hash_map = hash_map.lock().unwrap(); | ||
for (hash, count) in hash_map.iter() { | ||
let hash_type = match hash.len() { | ||
64 => "SHA256", | ||
32 => "MD5", | ||
_ => "Unknown" | ||
}; | ||
println!("{}: {}: {}", hash_type, hash, count); | ||
for hash in hashes { | ||
println!("{}", hash); | ||
} | ||
|
||
// Wait for user input to prevent the console window from closing | ||
print!("Press enter to exit..."); | ||
io::stdout().flush()?; | ||
let _ = io::stdin().read(&mut [0u8])?; | ||
|
||
Ok(()) | ||
} |