Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed May 13, 2023
1 parent c68c971 commit 68c733c
Show file tree
Hide file tree
Showing 26 changed files with 818 additions and 233 deletions.
34 changes: 15 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,26 @@ default = ["wee_alloc"]
lto = true

[dependencies]
# The `wasm-bindgen` crate provides the bare minimum functionality needed
# to interact with JavaScript.
wasm-bindgen = "0.2.85"
md-5 = "0.10.5"
sha1 = "0.10.5"
sha2 = "0.10.5"
sha3 = "0.10.5"
sm3 = "0.4.1"
js-sys = "0.3.62"
digest = "0.10.6"
wasm-bindgen-futures = "0.4.35"
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. However, it is slower than the default
# allocator, so it's not enabled by default.
wee_alloc = { version = "0.4.2", optional = true }
wasm-hasher-md5 = { path = "./crate/md5", default-features = false }
wasm-hasher-sha1 = { path = "./crate/sha1", default-features = false }
wasm-hasher-sha2 = { path = "./crate/sha2", default-features = false }
wasm-hasher-sha3 = { path = "./crate/sha3", default-features = false }
wasm-hasher-sm3 = { path = "./crate/sm3", default-features = false }

# The `web-sys` crate allows you to interact with the various browser APIs,
# like the DOM.
[dependencies.web-sys]
version = "0.3.61"
features = ["console", "FileReader", "Blob", "AbortSignal"]

# These crates are used for running unit tests.
[dev-dependencies]
wasm-bindgen-test = "0.2.45"
futures = "0.1.27"
js-sys = "0.3.62"

[workspace]
members = [
"crate/lib",
"crate/md5",
"crate/sha1",
"crate/sha2",
"crate/sha3",
"crate/sm3",
]
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.DEFAULT:
build:
@wasm-pack build --release --target bundler --scope axetroy --out-dir out
@wasm-pack build --release --target bundler --scope axetroy --out-dir out ./crate/md5
@wasm-pack build --release --target bundler --scope axetroy --out-dir out ./crate/sha1
@wasm-pack build --release --target bundler --scope axetroy --out-dir out ./crate/sha2
@wasm-pack build --release --target bundler --scope axetroy --out-dir out ./crate/sha3
@wasm-pack build --release --target bundler --scope axetroy --out-dir out ./crate/sm3
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The difference with [github.com/fuyoo/wasm-hasher](https://github.com/fuyoo/wasm
1. Hash process can be interrupted with `abortController`
2. The exposed function can specify the chunks size for each read. Larger chunks use more CPU.
3. `onProgress` callback is optional. Set to `null` to improve performance if you don't need it.
4. separate packages, smaller wasm files

## Usage

Expand All @@ -34,8 +35,24 @@ import("@axetroy/wasm-hasher").then(({ default: hasher }) => {

```bash
npm install @axetroy/wasm-hasher
npm install @axetroy/wasm-hasher-md5
npm install @axetroy/wasm-hasher-sha1
npm install @axetroy/wasm-hasher-sha2
npm install @axetroy/wasm-hasher-sha3
npm install @axetroy/wasm-hasher-sm3
```

### Packages

| Package | Description | Version |
| ------------------------- | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| @axetroy/wasm-hasher | Including all supported hash | [![npm version](https://badge.fury.io/js/@axetroy%2Fwasm-hasher.svg)](https://badge.fury.io/js/@axetroy%2Fwasm-hasher) |
| @axetroy/wasm-hasher-md5 | Including md5 hash | [![npm version](https://badge.fury.io/js/@axetroy%2Fwasm-hasher.svg)](https://badge.fury.io/js/@axetroy%2Fwasm-hasher-md5) |
| @axetroy/wasm-hasher-sha1 | Including sha1 hash | [![npm version](https://badge.fury.io/js/@axetroy%2Fwasm-hasher.svg)](https://badge.fury.io/js/@axetroy%2Fwasm-hasher-sha1) |
| @axetroy/wasm-hasher-sha2 | Including sha2 hash | [![npm version](https://badge.fury.io/js/@axetroy%2Fwasm-hasher.svg)](https://badge.fury.io/js/@axetroy%2Fwasm-hasher-sha2) |
| @axetroy/wasm-hasher-sha3 | Including sha3 hash | [![npm version](https://badge.fury.io/js/@axetroy%2Fwasm-hasher.svg)](https://badge.fury.io/js/@axetroy%2Fwasm-hasher-sha3) |
| @axetroy/wasm-hasher-sm3 | Including sm3 hash | [![npm version](https://badge.fury.io/js/@axetroy%2Fwasm-hasher.svg)](https://badge.fury.io/js/@axetroy%2Fwasm-hasher-sm3) |

### 🛠️ Build from source

Make sure you have install [rust^1.69](https://www.rust-lang.org/) and [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/)
Expand Down
36 changes: 36 additions & 0 deletions crate/lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "wasm-hasher-lib"
version = "0.1.0"
authors = ["Axetroy <axetroy.dev@gmail.com>"]
edition = "2021"
repository = "https://github.com/axetroy/wasm-hasher"
readme = "README.md"
description = "computed data hash by webAssembly"
license = "Anti-996"
keywords = ["wasm", "webassembly"]

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = []

[dependencies]
# The `wasm-bindgen` crate provides the bare minimum functionality needed
# to interact with JavaScript.
wasm-bindgen = "0.2.85"
js-sys = "0.3.62"
digest = "0.10.6"
wasm-bindgen-futures = "0.4.35"

# The `web-sys` crate allows you to interact with the various browser APIs,
# like the DOM.
[dependencies.web-sys]
version = "0.3.61"
features = ["console", "FileReader", "Blob", "AbortSignal"]

# These crates are used for running unit tests.
[dev-dependencies]
wasm-bindgen-test = "0.2.45"
futures = "0.1.27"
js-sys = "0.3.62"
55 changes: 55 additions & 0 deletions crate/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use digest::Digest;
use js_sys::{Function, Uint8Array};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
use web_sys::{AbortSignal, Blob};

pub async fn computed<D: Digest>(
signal: AbortSignal,
blob: Blob,
chunk_size: Option<f64>,
cb: &Option<Function>,
hasher: &mut D,
) -> Result<(), JsValue> {
let size = blob.size();

let chunk = match chunk_size {
Some(size) => size,
None => (1024 * 1024 * 5) as f64,
};

let chunks = (size / chunk).ceil() as i64;
let mut start: f64 = 0.0;

for _ in 0..chunks {
if signal.aborted() {
return Err(JsValue::from("Signal has been abort!"));
}

let mut end = start + chunk;
end = if end >= size { size } else { end };
let data = blob
.slice_with_f64_and_f64(start, end)
.expect("slice blob failed!");
let buffer: JsValue = JsFuture::from(data.array_buffer())
.await
.expect("get arrayBuffer failed!");
hasher.update(&Uint8Array::new(&buffer).to_vec());
match cb {
Some(func) => {
let _ = func.call1(&JsValue::null(), &JsValue::from(start / size * 100.0));
}
None => (),
}
start += chunk;
}

match cb {
Some(func) => {
let _ = func.call1(&JsValue::null(), &JsValue::from(100.0));
}
None => (),
}

Ok(())
}
42 changes: 42 additions & 0 deletions crate/md5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "wasm-hasher-md5"
version = "0.1.0"
authors = ["Axetroy <axetroy.dev@gmail.com>"]
edition = "2021"
repository = "https://github.com/axetroy/wasm-hasher"
readme = "README.md"
description = "computed data hash by webAssembly"
license = "Anti-996"
keywords = ["md5", "wasm", "webassembly"]

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["wee_alloc"]

[dependencies]
# The `wasm-bindgen` crate provides the bare minimum functionality needed
# to interact with JavaScript.
wasm-bindgen = "0.2.85"
md-5 = "0.10.5"
js-sys = "0.3.62"
digest = "0.10.6"
wasm-bindgen-futures = "0.4.35"
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. However, it is slower than the default
# allocator, so it's not enabled by default.
wee_alloc = { version = "0.4.2", optional = true }
wasm-hasher-lib = { path = "../lib" }

# The `web-sys` crate allows you to interact with the various browser APIs,
# like the DOM.
[dependencies.web-sys]
version = "0.3.61"
features = ["console", "FileReader", "Blob", "AbortSignal"]

# These crates are used for running unit tests.
[dev-dependencies]
wasm-bindgen-test = "0.2.45"
futures = "0.1.27"
js-sys = "0.3.62"
46 changes: 46 additions & 0 deletions crate/md5/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Copyright (c) 2023 Axetroy

Anti 996 License Version 1.0 (Draft)

Permission is hereby granted to any individual or legal entity
obtaining a copy of this licensed work (including the source code,
documentation and/or related items, hereinafter collectively referred
to as the "licensed work"), free of charge, to deal with the licensed
work for any purpose, including without limitation, the rights to use,
reproduce, modify, prepare derivative works of, distribute, publish
and sublicense the licensed work, subject to the following conditions:

1. The individual or the legal entity must conspicuously display,
without modification, this License and the notice on each redistributed
or derivative copy of the Licensed Work.

2. The individual or the legal entity must strictly comply with all
applicable laws, regulations, rules and standards of the jurisdiction
relating to labor and employment where the individual is physically
located or where the individual was born or naturalized; or where the
legal entity is registered or is operating (whichever is stricter). In
case that the jurisdiction has no such laws, regulations, rules and
standards or its laws, regulations, rules and standards are
unenforceable, the individual or the legal entity are required to
comply with Core International Labor Standards.

3. The individual or the legal entity shall not induce or force its
employee(s), whether full-time or part-time, or its independent
contractor(s), in any methods, to agree in oral or written form, to
directly or indirectly restrict, weaken or relinquish his or her
rights or remedies under such laws, regulations, rules and standards
relating to labor and employment as mentioned above, no matter whether
such written or oral agreement are enforceable under the laws of the
said jurisdiction, nor shall such individual or the legal entity
limit, in any methods, the rights of its employee(s) or independent
contractor(s) from reporting or complaining to the copyright holder or
relevant authorities monitoring the compliance of the license about
its violation(s) of the said license.

THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE
LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.
5 changes: 5 additions & 0 deletions crate/md5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Usage

```js
import pkg from "@axetroy/wasm-hasher-md5";
```
27 changes: 27 additions & 0 deletions crate/md5/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use digest::Digest;
use js_sys::Function;
use md5::Md5;
use wasm_bindgen::prelude::*;
use web_sys::{AbortSignal, Blob};

// When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
// allocator.
//
// If you don't want to use `wee_alloc`, you can safely delete this.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub async fn md5(
signal: AbortSignal,
blob: Blob,
chunk: Option<f64>,
on_progress: Option<Function>,
) -> Result<String, JsValue> {
let mut hasher = Md5::new();
match wasm_hasher_lib::computed(signal, blob, chunk, &on_progress, &mut hasher).await {
Ok(_) => Ok(format!("{:x}", hasher.finalize())),
Err(err) => Err(err),
}
}
42 changes: 42 additions & 0 deletions crate/sha1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "wasm-hasher-sha1"
version = "0.1.0"
authors = ["Axetroy <axetroy.dev@gmail.com>"]
edition = "2021"
repository = "https://github.com/axetroy/wasm-hasher"
readme = "README.md"
description = "computed data hash by webAssembly"
license = "Anti-996"
keywords = ["sha1", "wasm", "webassembly"]

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["wee_alloc"]

[dependencies]
# The `wasm-bindgen` crate provides the bare minimum functionality needed
# to interact with JavaScript.
wasm-bindgen = "0.2.85"
sha1 = "0.10.5"
js-sys = "0.3.62"
digest = "0.10.6"
wasm-bindgen-futures = "0.4.35"
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. However, it is slower than the default
# allocator, so it's not enabled by default.
wee_alloc = { version = "0.4.2", optional = true }
wasm-hasher-lib = { path = "../lib" }

# The `web-sys` crate allows you to interact with the various browser APIs,
# like the DOM.
[dependencies.web-sys]
version = "0.3.61"
features = ["console", "FileReader", "Blob", "AbortSignal"]

# These crates are used for running unit tests.
[dev-dependencies]
wasm-bindgen-test = "0.2.45"
futures = "0.1.27"
js-sys = "0.3.62"
46 changes: 46 additions & 0 deletions crate/sha1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Copyright (c) 2023 Axetroy

Anti 996 License Version 1.0 (Draft)

Permission is hereby granted to any individual or legal entity
obtaining a copy of this licensed work (including the source code,
documentation and/or related items, hereinafter collectively referred
to as the "licensed work"), free of charge, to deal with the licensed
work for any purpose, including without limitation, the rights to use,
reproduce, modify, prepare derivative works of, distribute, publish
and sublicense the licensed work, subject to the following conditions:

1. The individual or the legal entity must conspicuously display,
without modification, this License and the notice on each redistributed
or derivative copy of the Licensed Work.

2. The individual or the legal entity must strictly comply with all
applicable laws, regulations, rules and standards of the jurisdiction
relating to labor and employment where the individual is physically
located or where the individual was born or naturalized; or where the
legal entity is registered or is operating (whichever is stricter). In
case that the jurisdiction has no such laws, regulations, rules and
standards or its laws, regulations, rules and standards are
unenforceable, the individual or the legal entity are required to
comply with Core International Labor Standards.

3. The individual or the legal entity shall not induce or force its
employee(s), whether full-time or part-time, or its independent
contractor(s), in any methods, to agree in oral or written form, to
directly or indirectly restrict, weaken or relinquish his or her
rights or remedies under such laws, regulations, rules and standards
relating to labor and employment as mentioned above, no matter whether
such written or oral agreement are enforceable under the laws of the
said jurisdiction, nor shall such individual or the legal entity
limit, in any methods, the rights of its employee(s) or independent
contractor(s) from reporting or complaining to the copyright holder or
relevant authorities monitoring the compliance of the license about
its violation(s) of the said license.

THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE
LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.

0 comments on commit 68c733c

Please sign in to comment.