Skip to content

Commit

Permalink
Added support for "full" and "lite" version
Browse files Browse the repository at this point in the history
- Conditional compiling if you want to use one code or another.
- Added belT algorithm
  • Loading branch information
sbritorodr committed Oct 21, 2022
1 parent 216694a commit 2df5114
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 21 deletions.
60 changes: 59 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,69 @@ grid-row-gap: 0px;">
Advanced and fast hash generator/checker for files and plain text.
Using Tauri!
# Build:
It's important to add this tauri plugin before starting. Should be installed by running `npm install` inside the project dir.
1. It's important to add this tauri plugin before starting. Should be installed by running `npm install` inside the project dir.

`pnpm add github:tauri-apps/tauri-plugin-fs-extra`

`npm install github:tauri-apps/tauri-plugin-fs-extra`

# Contribute:
## Add more algorithms:
All extra hash functions should be added only for the full version. If you think a function is famous enough to be bundled inside all please discuss it in you PR.

I've tried to make adding more and mroe functions as easy as possible, though isn't something like a plugin but more than a extra code to be added.

1. Check if the hash is in [crates.io](crates.io)
2. Go to `src-tauri\Cargo.toml`, and edit this line under `[dependencies]`: You should add the `optional = "true"` line. For example, let's call a brand new hash "foo"
```toml
## Hashes:
md5 = "0.7.0" # MIT
# ...
whirlpool = { version ="0.10.1", optional = true } # MIT
belt-hash = {version = "0.1.0", optional = true} # MIT or APACHE2
# add here other hash functions:
foo = {version "x.x.x", optional = true}
```
3. Edit this line in `Cargo.toml`:
```toml
[features]
full = ["dep:blake3", "dep:whirlpool", "dep:belt-hash", "dep:foo"]
```
4. Go to `src-tauri\src\main.rs`. Add a new entry inside `bytes_hash_processing()`:
```rust
#[cfg(feature="full")]
fn bytes_hash_processing(input: &[u8], hashType: &str) -> OutputToJS {
let now = Instant::now();
let output = match hashType {
"md5" => allsum_md5(input),
// some stuff ommited here
"foo" => allsum_foo(input),
_ => format!("Hash type '{}' is not avaliable", hashType)
};
```
5. Go to `src-tauri\src\algorithms.rs` and create a new function at the end of the file. <br>The function **must** have a `[&u8]` input and a `String` output. The function content doesn't matter if the hash works for text and files. <br>If your hash function is using the `Digest` crate or it's inside [RustCrypto/hashes](https://github.com/RustCrypto/hashes) repo, it's easy to implement!:
```rust
#[cfg(feature="full")]
pub fn allsum_foo(foo_input:&[u8])-> String {
eprintln!("Generating foo output");
let mut hasher = foo_hash::FooHash::new();
hasher.update(foo_input);
let result = hasher.finalize();
format!("{:x}", result)
}
```
6. Go to `src-tauri/src/features.rs` and add the new hash inside `algorithms_selector_string()` function (**THE FULL VERSION FUNCTION, NOT THE SECOND ONE!**). The function should have `#[cfg(feature="full")]` at the top!:

```rust
#[cfg(feature="full")] // FULL VERSION
#[tauri::command]
pub fn algorithms_selector_string() -> String{
let enabled_algorithms:Vec<&str> = vec![..., "foo",];
...
}
```
7. Congrats! Your hash function is added!. Now test it with `pnpm tauri dev -f full`


# Copyleft licenses:
- Donate symbol: <div>Icon made from <a href="http://www.onlinewebfonts.com/icon">Icon Fonts</a> is licensed by CC BY 3.0</div>
8 changes: 5 additions & 3 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ tauri = { version = "1.1", features = ["dialog-open", "fs-read-file", "os-all",
wasm-typescript-definition = "0.1.4"
wasm-bindgen = "0.2.83"

## Hashes:
## Hashes: (Don't forget to add the name in ../src/features.rs)
md5 = "0.7.0" # MIT
sha-1 = "0.10.0" # MT
sha2 = "0.10.2" # MIT
blake3 = "1.3.1" # CCO
blake3 = { version ="1.3.1", optional = true } # CCO
crc32fast = "1.3.2" # MIT
whirlpool = "0.10.1" # MIT
whirlpool = { version ="0.10.1", optional = true } # MIT
belt-hash = {version = "0.1.0", optional = true} # MIT or APACHE2

[dependencies.tauri-plugin-fs-extra]
git = "https://github.com/tauri-apps/tauri-plugin-fs-extra"

[features]
full = ["dep:blake3", "dep:whirlpool", "dep:belt-hash"] # enables full version (all checksums enabled)
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = [ "custom-protocol" ]
Expand Down
33 changes: 24 additions & 9 deletions src-tauri/src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use md5;
use sha1::{self, Sha1};
use sha2::{Sha256, Sha512, Digest};
use blake3;
use crc32fast;
use whirlpool;
// Full version algorithms:
#[cfg(feature="full")]
use {
whirlpool,
blake3,
belt_hash,
};

// All algorithms are inside this list: https://github.com/RustCrypto/hashes
// There are some algorithms, such us BLAKE 3 that aren't in this list
Expand Down Expand Up @@ -34,22 +39,32 @@ pub fn allsum_sha512(sha512_input: &[u8]) -> String {
let result = hasher.finalize();
format!("{:x}", result)
}

pub fn allsum_blake3(blake3_input: &[u8]) -> String {
eprintln!("Generating BLAKE-3 algorithm...");
let result = blake3::hash(blake3_input);
format!("{}", result.to_hex())
}


pub fn allsum_md5(md5_input: &[u8]) -> String{ // For some reason, this website is wrong and it's at the first search result in google if you search "md5 online" https://www.md5.cz/
eprintln!("Generating md5 output...");
let digest = md5::compute(md5_input);
format!("{:x}", digest)
}
// All algorithms below here are only avaliable in full edition.
#[cfg(feature="full")]
pub fn allsum_blake3(blake3_input: &[u8]) -> String {
eprintln!("Generating BLAKE-3 algorithm...");
let result = blake3::hash(blake3_input);
format!("{}", result.to_hex())
}
#[cfg(feature="full")]
pub fn allsum_whirpool(whirlpool_input: &[u8]) -> String {
eprintln!("Generating whirpool output");
let mut hasher = whirlpool::Whirlpool::new();
hasher.update(whirlpool_input);
let result = hasher.finalize();
format!("{:x}", result)
}
#[cfg(feature="full")]
pub fn allsum_belT(belT_input:&[u8])-> String {
eprintln!("Generating belT output");
let mut hasher = belt_hash::BeltHash::new();
hasher.update(belT_input);
let result = hasher.finalize();
format!("{:x}", result)
}
37 changes: 37 additions & 0 deletions src-tauri/src/features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// "full" feature is a free edition for allsum that uses all checksums avaliable
// on rust. I'ts made for users with better storage.

// sorting code for play.rust-lang.org:

/*
fn main() {
let mut vector = vec!["crc32", "md5", "sha1", "sha256", "sha512"];
vector.sort();
println!("{:?}", vector)
}
*/

#[cfg(feature="full")] // FULL VERSION
#[tauri::command]
pub fn algorithms_selector_string() -> String{
// to avoid unnecesary sort functions, sort this outside if you want
let enabled_algorithms:Vec<&str> = vec!["belT", "blake3", "crc32", "md5", "sha1", "sha256", "sha512", "whirlpool"];
let mut innerHTML:String = String::new();
for algorithmTag in enabled_algorithms{
innerHTML.push_str(format!("<option value={}>{}</option>\n", algorithmTag, algorithmTag).as_str())
}
innerHTML
}

#[cfg(not(feature="full"))] // essentials VERSION
#[tauri::command]
pub fn algorithms_selector_string() -> String{
// to avoid unnecesary sort functions, sort this outside
let enabled_algorithms:Vec<&str> = vec!["crc32", "md5", "sha1", "sha256", "sha512"]; // to avoid unnecesary sort functions, sort this outside
let mut innerHTML:String = String::new();
for algorithmTag in enabled_algorithms{
innerHTML.push_str(format!("<option value={}>{}</option>\n", algorithmTag, algorithmTag).as_str())
}
innerHTML
}

36 changes: 31 additions & 5 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
// For some reason™️, tauri converts variables from snake_case to camelCase, creating an error of unused keys

use std::time::{Instant};
use features::algorithms_selector_string;
use serde::{Serialize, Deserialize}; //by looking at .toml, serde_json is enabled
use file::readFilefromPath;
use std::fs;
mod file;
mod algorithms;
use algorithms::*;
use tauri_plugin_fs_extra::FsExtra;
use tauri::Manager;
use wasm_typescript_definition::TypescriptDefinition;
use wasm_bindgen::prelude::wasm_bindgen;
//use std::thread;

mod file;
mod algorithms;
mod features;

#[derive(TypescriptDefinition, Serialize, Deserialize)]
#[serde(tag = "tag", content = "fields")]
enum OutputHash{
Expand Down Expand Up @@ -60,27 +63,50 @@ fn text_hash_processing(inputStr: &str, hashType: &str, isFileModeOn: bool) -> S
};
bytes_hash_processing(&input_bytes, hashType).hash // ToDo: Complete rework of etime
}

#[cfg(not(feature="full"))]
fn bytes_hash_processing(input: &[u8], hashType: &str) -> OutputToJS { //Separated in order to be re-used if I add the sum file mode instead of text
let now = Instant::now();
let output = match hashType {
"md5" => allsum_md5(input),
"sha1" => allsum_sha1(input),
"sha256" => allsum_sha256(input),
"sha512" => allsum_sha512(input),
"blake3" => allsum_blake3(input),
"crc32" => allsum_crc32(input),
_ => format!("Hash type '{}' is not avaliable", hashType)
};
let time_elapsed_ms:String = now.elapsed().as_millis().to_string(); //needs to be a string. JavaScript doesn't support u128
println!("Time Consumption: {} milliseconds", time_elapsed_ms);
OutputToJS { hash: output, etime: time_elapsed_ms }
}
#[cfg(feature="full")]
fn bytes_hash_processing(input: &[u8], hashType: &str) -> OutputToJS { //Separated in order to be re-used if I add the sum file mode instead of text
let now = Instant::now();
let output = match hashType {
"md5" => allsum_md5(input),
"sha1" => allsum_sha1(input),
"sha256" => allsum_sha256(input),
"sha512" => allsum_sha512(input),
"crc32" => allsum_crc32(input),
"blake3" => allsum_blake3(input),
"whirlpool" => allsum_whirpool(input),
"belT" => allsum_belT(input),
_ => format!("Hash type '{}' is not avaliable", hashType)
};
let time_elapsed_ms:String = now.elapsed().as_millis().to_string(); //needs to be a string. JavaScript doesn't support u128
println!("Time Consumption: {} milliseconds", time_elapsed_ms);
OutputToJS { hash: output, etime: time_elapsed_ms }
}


fn main() {
#[cfg(feature="full")]
println!("Full Edition is enabled for production!");
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![text_hash_processing, close_splashscreen, open_splashscreen]) //add all used commands here to communicate to js
.invoke_handler(tauri::generate_handler![
text_hash_processing,
close_splashscreen,
open_splashscreen,
algorithms_selector_string]) //add all used commands here to communicate to js
.plugin(FsExtra::default())
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"minWidth": 881,
"minHeight": 540,
"visible": false,
"label": "main"
"label": "main",
"decorations": true
},
{
"width": 800,
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export async function getTextInput() {
});
//invoke('open_splashscreen')
}


// HTML change functions
export function hashMode() {
let isFileModeOn = document.querySelector("#switch-button-checkbox")! as HTMLInputElement; // false == text mode
console.log(typeof isFileModeOn.checked, "Is file mode on?: ",isFileModeOn.checked);
Expand All @@ -101,6 +104,10 @@ export function hashMode() {
document.getElementById('text_input')?.removeEventListener("click", inputFileProcess);
}
}
// changes all algoritms depending on if you're using full or essentials version
async function algorithm_selection(){
document.getElementById('hash_type')!.innerHTML = await invoke('algorithms_selector_string')
}


// FILE MANAGE:
Expand Down Expand Up @@ -174,7 +181,8 @@ export function copyToClipboard() {
document.addEventListener('DOMContentLoaded', () => {
// This will wait for the window to load, but you could
// run this function on whatever trigger you want
//invoke('close_splashscreen')
})
invoke('close_splashscreen')
});
algorithm_selection();
//// Debug Only:
//getSystemInfo()

0 comments on commit 2df5114

Please sign in to comment.