Skip to content

Commit 3f5c5f3

Browse files
committed
prepare for release v3.0.1
1 parent b12ecb5 commit 3f5c5f3

11 files changed

+186
-201
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file. The format
44

55
## [Released]
66

7+
## [3.0.1] - 2024-12-08
8+
9+
### Changed
10+
- ``local`` subcommand
11+
- option `--file` was renamed to `--path`
12+
- now you use a path pointing to a file or a directory and calculate a hash sum
13+
- Refactoring
14+
<br>
15+
716
## [3.0.0] - 2024-11-08
817

918
### Changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hashguard"
3-
version = "3.0.0"
3+
version = "3.0.1"
44
edition = "2021"
55
description = "Command-Line tool for ensuring the integrity of files using hash sums"
66
authors = ["javaLux"]
@@ -12,7 +12,6 @@ categories = ["command-line-utilities", "development-tools", "cryptography"]
1212
rust-version = "1.74.0"
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
15-
1615
[dependencies]
1716
better-panic = "0.3.0"
1817
chksum = { version = "0.3.0", default-features = false, features = ["md5", "sha1", "sha2-224", "sha2-256", "sha2-384", "sha2-512"] }
@@ -36,7 +35,7 @@ url = "2.5.3"
3635
[profile.release]
3736
# compiler optimizations for binary size
3837
opt-level = "s"
39-
# link time optimizations
38+
# link optimizations -> causes a longer link time but produce better optimize code
4039
lto = true
4140
# strip either symbols or debuginfo from the final binary
4241
strip = true

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ HashGuard is a lean and efficient command-line tool designed to simplify the pro
3232
Because by enclosing the URL in double quotation marks, you tell the shell to treat the entire string as a single argument, even if it contains spaces or other special characters. This can prevent errors and unexpected behavior in your shell.
3333

3434
* ### Local-Command
35-
* Allows to hash local files or any byte-buffer (futhermore you can also compare with a specific hash sum)
35+
* Allows to hash local files, directories or any byte-buffer (futhermore you can also compare with a specific hash sum)
3636
* **Options**
37-
* _file_
38-
* Calculate a hash sum from a file on your local system
37+
* _path_
38+
* Calculate a hash sum from a file/dir
3939
* _buffer_
4040
* Calculate a hash sum from any given byte buffer
4141
* What means byte buffer?
4242
* For example, you can calculate a hash sum from any text that is provided as a ``String``
4343
* As described in the download command, please enclose the text to be hashed in double quotation marks. This prevents unexpected behavior in your shell.
44-
* _Notice_
45-
* You can only use one option per call. So either ``file`` or ``buffer``
44+
* _Notice_
45+
* You can only use one option per call. So either ``path`` or ``buffer``
4646

4747

4848
* **Hash Verification:** Verify the authenticity of downloaded or local files by comparing their hash sum with a provided hash value.
@@ -122,17 +122,17 @@ cargo build --release
122122
**Local-Command**
123123
* Verify a local file with a hash sum using SHA-1:
124124
````shell
125-
hashguard local -f /path/to/local_file.txt a1b2c3d4e5f6 -a sha1
125+
hashguard local -p /path/to/local_file.txt a1b2c3d4e5f6 -a sha1
126126
````
127127

128128
* Calculate a hash sum from a given ``String``:
129129
````shell
130130
hashguard local -b "Hello young Padawan"
131131
````
132132

133-
* Calculate a hash sum from a local file with the default hash algorithm:
133+
* Calculate a hash sum from a local directory with the default hash algorithm:
134134
````shell
135-
hashguard local -f /path/to/local_file.txt
135+
hashguard local -p /path/to/test_dir
136136
````
137137

138138
**Debug-Option**
@@ -141,7 +141,7 @@ cargo build --release
141141
hashguard -l debug download "https://example.com/file.zip" a1b2c3d4e5f6
142142
````
143143
````shell
144-
hashguard -l debug local -f /path/to/local_file.txt
144+
hashguard -l debug local -p /path/to/local_file.txt
145145
````
146146
* In the event of an error, a full backtrace is displayed
147147
* In addition, all log outputs are saved in a log file in the application's data directory.

src/cli.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use clap::{Args, Parser, Subcommand};
24

35
use crate::{
@@ -24,7 +26,7 @@ pub struct Cli {
2426
pub enum Commands {
2527
/// Download a file and calculate a hash sum
2628
Download(DownloadArgs),
27-
/// Calculate a hash sum from a file or a byte buffer
29+
/// Calculate a hash sum from a file/dir or a byte buffer
2830
Local(LocalArgs),
2931
}
3032

@@ -51,7 +53,7 @@ pub struct DownloadArgs {
5153
help = "A custom path for the file to be saved (Default is the user download folder)",
5254
value_name = "DIR"
5355
)]
54-
pub output: Option<String>,
56+
pub output: Option<PathBuf>,
5557

5658
#[arg(
5759
short,
@@ -68,15 +70,17 @@ pub struct LocalArgs {
6870
short,
6971
long,
7072
conflicts_with = "buffer",
71-
help = "Path to the file for which the hash sum will be calculated"
73+
help = "Path to a file/dir for which the hash sum will be calculated",
74+
value_name = "PATH"
7275
)]
73-
pub file: Option<String>,
76+
pub path: Option<PathBuf>,
7477

7578
#[arg(
7679
short,
7780
long,
78-
conflicts_with = "file",
79-
help = "Buffer (e.g. String) for which the hash sum will be calculated"
81+
conflicts_with = "path",
82+
help = "Buffer (e.g. String) for which the hash sum will be calculated",
83+
value_name = "STRING"
8084
)]
8185
pub buffer: Option<String>,
8286

src/color_templates.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,15 @@ pub const INFO_TEMPLATE: ColoredTemplate = ColoredTemplate {
88
bg_color: Some(Color::Green),
99
style: Style::Bold,
1010
};
11-
#[allow(dead_code)]
12-
pub const WARN_TEMPLATE: ColoredTemplate = ColoredTemplate {
13-
ft_color: Color::White,
14-
bg_color: Some(Color::Yellow),
15-
style: Style::Bold,
16-
};
1711
pub const ERROR_TEMPLATE: ColoredTemplate = ColoredTemplate {
1812
ft_color: Color::White,
1913
bg_color: Some(Color::Red),
2014
style: Style::Bold,
2115
};
22-
#[allow(dead_code)]
23-
// These templates using no background color
24-
pub const INFO_TEMPLATE_NO_BG_COLOR: ColoredTemplate = ColoredTemplate {
25-
ft_color: Color::Green,
26-
bg_color: None,
27-
style: Style::Bold,
28-
};
16+
17+
// No background color used
2918
pub const WARN_TEMPLATE_NO_BG_COLOR: ColoredTemplate = ColoredTemplate {
3019
ft_color: Color::Yellow,
3120
bg_color: None,
3221
style: Style::Bold,
3322
};
34-
#[allow(dead_code)]
35-
pub const ERROR_TEMPLATE_NO_BG_COLOR: ColoredTemplate = ColoredTemplate {
36-
ft_color: Color::Red,
37-
bg_color: None,
38-
style: Style::Bold,
39-
};

src/commands.rs

+31-57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::error::Error;
22
use std::fmt;
3-
use std::path::{Path, PathBuf};
3+
use std::path::PathBuf;
44

55
use color_eyre::eyre::Result;
66

@@ -14,6 +14,7 @@ use crate::verify::{self, Algorithm};
1414
#[derive(Debug)]
1515
pub struct CommandResult {
1616
pub file_location: Option<PathBuf>,
17+
pub buffer_size: Option<usize>,
1718
pub used_algorithm: Algorithm,
1819
pub calculated_hash_sum: String,
1920
pub hash_compare_result: Option<HashCompareResult>,
@@ -27,7 +28,7 @@ pub struct HashCompareResult {
2728

2829
#[derive(Debug, PartialEq, PartialOrd)]
2930
pub enum CommandError {
30-
FileNotExist(String),
31+
PathNotExist(String),
3132
InvalidUrl,
3233
OutputTargetInvalid(String),
3334
RenameOptionInvalid(String),
@@ -38,12 +39,12 @@ impl Error for CommandError {}
3839
impl fmt::Display for CommandError {
3940
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4041
match self {
41-
CommandError::FileNotExist(file) => {
42-
let msg = format!("The specified file '{}' does not exist", file);
42+
CommandError::PathNotExist(path) => {
43+
let msg = format!("The specified path '{}' does not exist", path);
4344
write!(f, "{}", msg)
4445
}
4546
CommandError::InvalidUrl => {
46-
write!(f, "The provided URL is invalid. Please ensure the URL is correctly formatted,\nincluding the scheme (e.g. 'http://', 'https://').\nFor example: https://www.example.com")
47+
write!(f, "The provided URL is invalid. Please ensure the URL is correctly formatted,\nincluding the scheme (e.g. 'http://', 'https://').\nFor example: https://example.com")
4748
}
4849
CommandError::OutputTargetInvalid(target) => {
4950
let msg = format!(
@@ -71,11 +72,11 @@ pub fn handle_download_cmd(args: DownloadArgs, os_type: os_specifics::OS) -> Res
7172
let output_target = match output_target {
7273
Some(output_target) => {
7374
// only a existing directory is valid as an output target
74-
let p = Path::new(&output_target);
75-
if p.is_dir() {
76-
p.to_path_buf()
75+
if output_target.is_dir() {
76+
output_target
7777
} else {
78-
let command_err = CommandError::OutputTargetInvalid(utils::get_absolute_path(p));
78+
let command_err =
79+
CommandError::OutputTargetInvalid(utils::get_absolute_path(&output_target));
7980
return Err(color_eyre::eyre::eyre!(command_err.to_string()));
8081
}
8182
}
@@ -119,65 +120,36 @@ pub fn handle_download_cmd(args: DownloadArgs, os_type: os_specifics::OS) -> Res
119120
// start the download
120121
let file_path = download::execute_download(download_properties)?;
121122

122-
// Reads the entire contents of the downloaded file into a bytes vector.
123-
let data_to_hash = std::fs::read(&file_path)?;
124-
125-
let mut calculated_hash_sum =
126-
verify::get_hash_sum_as_lower_hex(data_to_hash, Some(file_path.clone()), args.algorithm)?;
127-
128-
let cmd_result = if let Some(origin_hash_sum) = args.hash_sum {
129-
if !verify::is_lower_hex(&origin_hash_sum) {
130-
// convert the calculated hash sum to UpperHex
131-
calculated_hash_sum = calculated_hash_sum
132-
.chars()
133-
.map(|c| c.to_uppercase().to_string())
134-
.collect();
135-
}
136-
137-
let is_file_modified = verify::is_hash_equal(&origin_hash_sum, &calculated_hash_sum);
138-
CommandResult {
139-
file_location: Some(file_path),
140-
used_algorithm: args.algorithm,
141-
calculated_hash_sum,
142-
hash_compare_result: Some(HashCompareResult {
143-
is_file_modified,
144-
origin_hash_sum,
145-
}),
146-
}
147-
} else {
148-
CommandResult {
149-
file_location: Some(file_path),
150-
used_algorithm: args.algorithm,
151-
calculated_hash_sum,
152-
hash_compare_result: None,
153-
}
123+
let local_args = LocalArgs {
124+
path: Some(file_path),
125+
buffer: None,
126+
hash_sum: args.hash_sum,
127+
algorithm: args.algorithm,
154128
};
155-
utils::processing_cmd_result(cmd_result);
156-
Ok(())
129+
130+
handle_local_cmd(local_args)
157131
}
158132

159133
// Handle the CLI subcommand 'local'
160134
pub fn handle_local_cmd(args: LocalArgs) -> Result<()> {
161-
let (data_to_hash, file_path) = if let Some(file) = args.file {
162-
// check if the given file path point to an existing file
163-
let file_path = PathBuf::from(file);
164-
if file_path.exists() {
165-
let data_to_hash = std::fs::read(file_path.clone())?;
166-
(data_to_hash, Some(file_path))
135+
let (mut calculated_hash_sum, file_location, buffer_size) = if let Some(path) = args.path {
136+
// check if the given path point to an existing file or directory
137+
if path.exists() {
138+
let calculated_hash_sum = verify::get_file_hash(path.clone(), args.algorithm)?;
139+
(calculated_hash_sum, Some(path), None)
167140
} else {
168-
let command_err = CommandError::FileNotExist(utils::get_absolute_path(&file_path));
141+
let command_err = CommandError::PathNotExist(utils::get_absolute_path(&path));
169142
return Err(color_eyre::eyre::eyre!(command_err.to_string()));
170143
}
171144
} else if let Some(some_text) = args.buffer {
172-
let data_to_hash = some_text.as_bytes().to_vec();
173-
(data_to_hash, None)
145+
let buffer = some_text.as_bytes().to_vec();
146+
let buffer_size = buffer.len();
147+
let calculated_hash_sum = verify::get_buffer_hash(buffer, args.algorithm)?;
148+
(calculated_hash_sum, None, Some(buffer_size))
174149
} else {
175150
return Ok(());
176151
};
177152

178-
let mut calculated_hash_sum =
179-
verify::get_hash_sum_as_lower_hex(data_to_hash, file_path.clone(), args.algorithm)?;
180-
181153
let cmd_result = if let Some(origin_hash_sum) = args.hash_sum {
182154
if !verify::is_lower_hex(&origin_hash_sum) {
183155
// convert the calculated hash sum to UpperHex
@@ -189,7 +161,8 @@ pub fn handle_local_cmd(args: LocalArgs) -> Result<()> {
189161

190162
let is_file_modified = verify::is_hash_equal(&origin_hash_sum, &calculated_hash_sum);
191163
CommandResult {
192-
file_location: file_path,
164+
file_location,
165+
buffer_size,
193166
used_algorithm: args.algorithm,
194167
calculated_hash_sum,
195168
hash_compare_result: Some(HashCompareResult {
@@ -199,7 +172,8 @@ pub fn handle_local_cmd(args: LocalArgs) -> Result<()> {
199172
}
200173
} else {
201174
CommandResult {
202-
file_location: file_path,
175+
file_location,
176+
buffer_size,
203177
used_algorithm: args.algorithm,
204178
calculated_hash_sum,
205179
hash_compare_result: None,

src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[macro_use]
22
extern crate lazy_static;
33

4-
// make own custom modules available
54
mod app;
65
mod cli;
76
mod color_templates;

src/tests/unit_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ pub mod unit_tests {
2323
}
2424

2525
#[test]
26-
fn test_upper_hex() {
27-
use verify::is_upper_hex;
28-
let check_sum = "A92fAE5G42B9F444";
29-
assert!(is_upper_hex(check_sum));
26+
fn ne_is_lower_hex() {
27+
use verify::is_lower_hex;
28+
let check_sum = "A92FAE5G42B9F444";
29+
assert!(!is_lower_hex(check_sum));
3030
}
3131

3232
#[test]

0 commit comments

Comments
 (0)