-
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.
- Loading branch information
Showing
26 changed files
with
818 additions
and
233 deletions.
There are no files selected for viewing
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
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,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 |
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
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 |
---|---|---|
@@ -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" |
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 |
---|---|---|
@@ -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(()) | ||
} |
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 |
---|---|---|
@@ -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" |
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 |
---|---|---|
@@ -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. |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### Usage | ||
|
||
```js | ||
import pkg from "@axetroy/wasm-hasher-md5"; | ||
``` |
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 |
---|---|---|
@@ -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), | ||
} | ||
} |
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 |
---|---|---|
@@ -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" |
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 |
---|---|---|
@@ -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. |
Oops, something went wrong.