mirror of
https://github.com/UneBaguette/blake3.wasm.git
synced 2026-08-07 00:00:16 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27e0ecc293 | ||
|
|
59cd38b35b
|
||
|
|
a841ee1313
|
||
|
|
ac63099741
|
||
|
|
2de1b09087
|
||
|
|
112db2e0fe
|
||
|
|
90e9a905b0
|
||
|
|
7e7ee43bad
|
||
|
|
d3871afb58
|
||
|
|
7fca25291c
|
||
|
|
4476a5bcc4
|
||
|
|
edc8284936
|
||
|
|
c9a6d8f39b | ||
|
|
ccb2d39c16 | ||
|
|
85b27b140d
|
||
|
|
7f8fb67345 | ||
|
|
99980eff38
|
||
|
|
7fec54a9b5
|
||
|
|
5c4c776268
|
||
|
|
9911c9309f
|
||
|
|
230976664c
|
||
|
|
4570d6826f
|
||
|
|
878508fbd3
|
@@ -12,22 +12,23 @@ env:
|
|||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10
|
||||||
|
|
||||||
- name: Install wasm-pack
|
- name: Install wasm-pack
|
||||||
run: |
|
run: |
|
||||||
curl -L https://github.com/rustwasm/wasm-pack/releases/download/v0.14.0/wasm-pack-v0.14.0-x86_64-unknown-linux-musl.tar.gz | tar xz
|
curl -L https://github.com/wasm-bindgen/wasm-pack/releases/download/v${{ vars.WASM_PACK_VERSION }}/wasm-pack-v${{ vars.WASM_PACK_VERSION }}-x86_64-unknown-linux-musl.tar.gz | tar xz
|
||||||
sudo mv wasm-pack-*/wasm-pack /usr/local/bin/
|
sudo mv wasm-pack-*/wasm-pack /usr/local/bin/
|
||||||
|
|
||||||
- name: Install wasm-bindgen-cli
|
- name: Install wasm-bindgen-cli
|
||||||
run: |
|
run: |
|
||||||
curl -L https://github.com/wasm-bindgen/wasm-bindgen/releases/download/0.2.118/wasm-bindgen-0.2.118-x86_64-unknown-linux-musl.tar.gz | tar xz
|
curl -L https://github.com/wasm-bindgen/wasm-bindgen/releases/download/${{ vars.WASM_BINDGEN_VERSION }}/wasm-bindgen-${{ vars.WASM_BINDGEN_VERSION }}-x86_64-unknown-linux-musl.tar.gz | tar xz
|
||||||
sudo mv wasm-bindgen-*/wasm-bindgen /usr/local/bin/
|
|
||||||
sudo mv wasm-bindgen-*/wasm-bindgen-test-runner /usr/local/bin/
|
sudo mv wasm-bindgen-*/wasm-bindgen-test-runner /usr/local/bin/
|
||||||
|
|
||||||
- name: Rust tests
|
- name: Rust tests
|
||||||
run: cargo test
|
run: cargo test
|
||||||
|
|
||||||
- name: WASM tests (Node)
|
- name: WASM tests (Node)
|
||||||
run: wasm-pack test --node
|
run: wasm-pack test --node --features wasm,talc
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
name: Publish
|
name: Publish
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
release:
|
||||||
tags: [ "v*" ]
|
types: [ published ]
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
@@ -12,38 +12,42 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10
|
||||||
|
|
||||||
|
- name: Setup Node
|
||||||
|
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4
|
||||||
|
with:
|
||||||
|
node-version: '24.16.0'
|
||||||
|
registry-url: 'https://registry.npmjs.org'
|
||||||
|
|
||||||
- name: Install wasm-pack
|
- name: Install wasm-pack
|
||||||
run: |
|
run: |
|
||||||
curl -L https://github.com/wasm-bindgen/wasm-pack/releases/download/v0.14.0/wasm-pack-v0.14.0-x86_64-unknown-linux-musl.tar.gz | tar xz
|
curl -L https://github.com/wasm-bindgen/wasm-pack/releases/download/v${{ vars.WASM_PACK_VERSION }}/wasm-pack-v${{ vars.WASM_PACK_VERSION }}-x86_64-unknown-linux-musl.tar.gz | tar xz
|
||||||
sudo mv wasm-pack-*/wasm-pack /usr/local/bin/
|
sudo mv wasm-pack-*/wasm-pack /usr/local/bin/
|
||||||
|
|
||||||
- name: Install wasm-bindgen-cli
|
- name: Install wasm-bindgen-cli
|
||||||
run: |
|
run: |
|
||||||
curl -L https://github.com/wasm-bindgen/wasm-bindgen/releases/download/0.2.118/wasm-bindgen-0.2.118-x86_64-unknown-linux-musl.tar.gz | tar xz
|
curl -L https://github.com/wasm-bindgen/wasm-bindgen/releases/download/${{ vars.WASM_BINDGEN_VERSION }}/wasm-bindgen-${{ vars.WASM_BINDGEN_VERSION }}-x86_64-unknown-linux-musl.tar.gz | tar xz
|
||||||
sudo mv wasm-bindgen-*/wasm-bindgen /usr/local/bin/
|
sudo mv wasm-bindgen-*/wasm-bindgen /usr/local/bin/
|
||||||
sudo mv wasm-bindgen-*/wasm-bindgen-test-runner /usr/local/bin/
|
sudo mv wasm-bindgen-*/wasm-bindgen-test-runner /usr/local/bin/
|
||||||
|
|
||||||
- name: Install wasm-opt
|
- name: Install wasm-opt
|
||||||
run: |
|
run: |
|
||||||
curl -L https://github.com/WebAssembly/binaryen/releases/latest/download/binaryen-version_129-x86_64-linux.tar.gz | tar xz
|
curl -L https://github.com/WebAssembly/binaryen/releases/download/version_${{ vars.WASM_OPT_VERSION }}/binaryen-version_${{ vars.WASM_OPT_VERSION }}-x86_64-linux.tar.gz | tar xz
|
||||||
sudo mv binaryen-*/bin/wasm-opt /usr/local/bin/
|
sudo mv binaryen-*/bin/wasm-opt /usr/local/bin/
|
||||||
|
|
||||||
- name: Rust tests
|
- name: Rust tests
|
||||||
run: cargo test
|
run: cargo test
|
||||||
|
|
||||||
- name: WASM tests (Node)
|
- name: WASM tests (Node)
|
||||||
run: wasm-pack test --node
|
run: wasm-pack test --node --features wasm,talc
|
||||||
|
|
||||||
- name: Build package
|
- name: Build package
|
||||||
run: make
|
run: make
|
||||||
|
|
||||||
- name: Publish to npm
|
- name: Publish to npm
|
||||||
run: |
|
run: |
|
||||||
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > pkg/.npmrc
|
|
||||||
cd pkg
|
cd pkg
|
||||||
npm publish --access public
|
npm publish
|
||||||
env:
|
|
||||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
||||||
|
|||||||
Generated
+33
-24
@@ -19,6 +19,9 @@ name = "arrayvec"
|
|||||||
version = "0.7.6"
|
version = "0.7.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
|
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
|
||||||
|
dependencies = [
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
@@ -39,9 +42,9 @@ checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "blake3"
|
name = "blake3"
|
||||||
version = "1.8.4"
|
version = "1.8.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4d2d5991425dfd0785aed03aedcf0b321d61975c9b5b3689c774a2610ae0b51e"
|
checksum = "0aa83c34e62843d924f905e0f5c866eb1dd6545fc4d719e803d9ba6030371fce"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayref",
|
"arrayref",
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
@@ -49,11 +52,12 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
"constant_time_eq",
|
"constant_time_eq",
|
||||||
"cpufeatures",
|
"cpufeatures",
|
||||||
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "blake3-wasm-rs"
|
name = "blake3-wasm-rs"
|
||||||
version = "0.5.1"
|
version = "0.6.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"blake3",
|
"blake3",
|
||||||
"hex",
|
"hex",
|
||||||
@@ -149,13 +153,12 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.95"
|
version = "0.3.100"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2964e92d1d9dc3364cae4d718d93f227e3abb088e747d92e0395bfdedf1c12ca"
|
checksum = "f2025f20d7a4fa7785846e7b63d10a76d3f1cee98ee5cb79ea59703f95e42162"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"once_cell",
|
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -340,9 +343,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "talc"
|
name = "talc"
|
||||||
version = "5.0.3"
|
version = "5.0.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3081e3f3ad542dc4cd0ae720f752598f8880fd364dc2d031132e3509f673aa23"
|
checksum = "40a5888b3189a89207eea4190e74387b5db3150d213f84186cac637dcd55259d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"allocator-api2",
|
"allocator-api2",
|
||||||
"lock_api",
|
"lock_api",
|
||||||
@@ -366,9 +369,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.118"
|
version = "0.2.123"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0bf938a0bacb0469e83c1e148908bd7d5a6010354cf4fb73279b7447422e3a89"
|
checksum = "a254a4b10c19a76f09a27640e7ffbf9bc30bf67e16a3bf28aaefa4920fe81563"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
@@ -379,9 +382,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-futures"
|
name = "wasm-bindgen-futures"
|
||||||
version = "0.4.68"
|
version = "0.4.73"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f371d383f2fb139252e0bfac3b81b265689bf45b6874af544ffa4c975ac1ebf8"
|
checksum = "54568702fabf5d4849ce2b90fadfa64168a097eaf4b351ce9df8b687a0086aaf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
@@ -389,9 +392,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro"
|
name = "wasm-bindgen-macro"
|
||||||
version = "0.2.118"
|
version = "0.2.123"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eeff24f84126c0ec2db7a449f0c2ec963c6a49efe0698c4242929da037ca28ed"
|
checksum = "24a40fc75b0ec6f3746ceb10d36f53a93dcd68a93b11b6445983945d79eba0dc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"wasm-bindgen-macro-support",
|
"wasm-bindgen-macro-support",
|
||||||
@@ -399,9 +402,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro-support"
|
name = "wasm-bindgen-macro-support"
|
||||||
version = "0.2.118"
|
version = "0.2.123"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9d08065faf983b2b80a79fd87d8254c409281cf7de75fc4b773019824196c904"
|
checksum = "908f34bd9b9ce3d4caf07b72dfab63d61504d156856c6bd3cd87fa350cf3985b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
@@ -412,18 +415,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-shared"
|
name = "wasm-bindgen-shared"
|
||||||
version = "0.2.118"
|
version = "0.2.123"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5fd04d9e306f1907bd13c6361b5c6bfc7b3b3c095ed3f8a9246390f8dbdee129"
|
checksum = "7acbf7616c27b194bbb550bf77ed0c2c3e5b7fd1260a93082b95fb7f47959b92"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-test"
|
name = "wasm-bindgen-test"
|
||||||
version = "0.3.68"
|
version = "0.3.73"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6bb55e2540ad1c56eec35fd63e2aea15f83b11ce487fd2de9ad11578dfc047ea"
|
checksum = "e28a0782b173cf2e98f62aacb487c5c021b7c5925df46098675f28e1fa85e159"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"cast",
|
"cast",
|
||||||
@@ -443,9 +446,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-test-macro"
|
name = "wasm-bindgen-test-macro"
|
||||||
version = "0.3.68"
|
version = "0.3.73"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "caf0ca1bd612b988616bac1ab34c4e4290ef18f7148a1d8b7f31c150080e9295"
|
checksum = "ee997551ce1ad5adda03f7ce37ec34b4140fe9f547fd07b46d55901d1ba1a06b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -454,9 +457,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-test-shared"
|
name = "wasm-bindgen-test-shared"
|
||||||
version = "0.2.118"
|
version = "0.2.123"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "23cda5ecc67248c48d3e705d3e03e00af905769b78b9d2a1678b663b8b9d4472"
|
checksum = "ae0f005eb61f765eff31a79ef71df7e21819731268a868df41192c1e41d6d3e5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-util"
|
name = "winapi-util"
|
||||||
@@ -482,6 +485,12 @@ dependencies = [
|
|||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zeroize"
|
||||||
|
version = "1.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zmij"
|
name = "zmij"
|
||||||
version = "1.0.21"
|
version = "1.0.21"
|
||||||
|
|||||||
+9
-6
@@ -1,12 +1,12 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "blake3-wasm-rs"
|
name = "blake3-wasm-rs"
|
||||||
version = "0.5.1"
|
version = "0.6.6"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT OR Apache-2.0"
|
||||||
authors = ["UneBaguette <[email protected]>"]
|
authors = ["Thomas <[email protected]>"]
|
||||||
publish = false
|
publish = false
|
||||||
repository = "https://github.com/UneBaguette/blake3.wasm"
|
repository = "https://github.com/UneBaguette/blake3.wasm"
|
||||||
description = "Simple blake3 to wasm project"
|
description = "BLAKE3 hashing with optional WASM bindings"
|
||||||
keywords = ["blake3", "wasm", "hash", "cryptography", "wasm-bindgen"]
|
keywords = ["blake3", "wasm", "hash", "cryptography", "wasm-bindgen"]
|
||||||
categories = ["cryptography", "wasm"]
|
categories = ["cryptography", "wasm"]
|
||||||
|
|
||||||
@@ -14,11 +14,14 @@ categories = ["cryptography", "wasm"]
|
|||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
default = []
|
||||||
|
wasm = ["dep:wasm-bindgen"]
|
||||||
talc = ["dep:talc"]
|
talc = ["dep:talc"]
|
||||||
|
zeroize = ["blake3/zeroize"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
blake3 = { version = "1.8", features = ["wasm32_simd"] }
|
blake3 = { version = "1", features = ["wasm32_simd"] }
|
||||||
wasm-bindgen = "=0.2.118"
|
wasm-bindgen = { version = "=0.2.123", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wasm-bindgen-test = "0.3"
|
wasm-bindgen-test = "0.3"
|
||||||
|
|||||||
+177
@@ -0,0 +1,177 @@
|
|||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2026 UneBaguette
|
Copyright (c) 2026 Thomas <tom@unebaguette.fr>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|
||||||
@@ -20,12 +20,13 @@ build: build-all node-esm
|
|||||||
|
|
||||||
build-all:
|
build-all:
|
||||||
@echo "Building all target..."
|
@echo "Building all target..."
|
||||||
RUSTFLAGS='-C opt-level=3 -C target-feature=+simd128' $(CARGO) build --target $(WASM_TARGET) --release --features talc
|
RUSTFLAGS='-C opt-level=3 -C target-feature=+simd128' $(CARGO) build --target $(WASM_TARGET) --release --features wasm,talc,zeroize
|
||||||
@mkdir -p $(ROOT)/bundler $(ROOT)/web $(ROOT)/node
|
@mkdir -p $(ROOT)/bundler $(ROOT)/web $(ROOT)/node
|
||||||
$(WASM_BINDGEN) --target bundler --out-dir $(ROOT)/bundler $(TARGET_DIR)/$(CRATE).wasm
|
$(WASM_BINDGEN) --target bundler --out-dir $(ROOT)/bundler $(TARGET_DIR)/$(CRATE).wasm
|
||||||
$(WASM_BINDGEN) --target web --out-dir $(ROOT)/web $(TARGET_DIR)/$(CRATE).wasm
|
$(WASM_BINDGEN) --target web --out-dir $(ROOT)/web $(TARGET_DIR)/$(CRATE).wasm
|
||||||
$(WASM_BINDGEN) --target nodejs --out-dir $(ROOT)/node $(TARGET_DIR)/$(CRATE).wasm
|
$(WASM_BINDGEN) --target nodejs --out-dir $(ROOT)/node $(TARGET_DIR)/$(CRATE).wasm
|
||||||
@mv $(ROOT)/bundler/$(CRATE)_bg.wasm $(ROOT)/$(CRATE)_bg.wasm
|
@mv $(ROOT)/bundler/$(CRATE)_bg.wasm $(ROOT)/$(CRATE)_bg.wasm
|
||||||
|
@cp $(ROOT)/bundler/$(CRATE)_bg.js $(ROOT)/$(CRATE)_bg.js
|
||||||
@rm -f $(ROOT)/web/$(CRATE)_bg.wasm $(ROOT)/node/$(CRATE)_bg.wasm
|
@rm -f $(ROOT)/web/$(CRATE)_bg.wasm $(ROOT)/node/$(CRATE)_bg.wasm
|
||||||
$(WASM_OPT) $(WASM_OPT_FLAGS) $(ROOT)/$(CRATE)_bg.wasm -o $(ROOT)/$(CRATE)_bg.wasm
|
$(WASM_OPT) $(WASM_OPT_FLAGS) $(ROOT)/$(CRATE)_bg.wasm -o $(ROOT)/$(CRATE)_bg.wasm
|
||||||
@node -e "['bundler','web','node'].forEach(d=>{const f='$(ROOT)/'+d+'/$(CRATE).js';require('fs').writeFileSync(f,require('fs').readFileSync(f,'utf8').replace(/$(CRATE)_bg\.wasm/g,'../$(CRATE)_bg.wasm'))})"
|
@node -e "['bundler','web','node'].forEach(d=>{const f='$(ROOT)/'+d+'/$(CRATE).js';require('fs').writeFileSync(f,require('fs').readFileSync(f,'utf8').replace(/$(CRATE)_bg\.wasm/g,'../$(CRATE)_bg.wasm'))})"
|
||||||
@@ -35,24 +36,27 @@ build-all:
|
|||||||
@cp scripts/tpl/index.js.template $(ROOT)/index.js
|
@cp scripts/tpl/index.js.template $(ROOT)/index.js
|
||||||
@cp scripts/tpl/index.d.ts $(ROOT)/index.d.ts
|
@cp scripts/tpl/index.d.ts $(ROOT)/index.d.ts
|
||||||
@cp README.md $(ROOT)/README.md
|
@cp README.md $(ROOT)/README.md
|
||||||
@cp LICENSE $(ROOT)/LICENSE
|
@cp LICENSE-MIT $(ROOT)/LICENSE-MIT
|
||||||
@sed -i 's|// @ts-nocheck|// Types|' $(ROOT)/index.d.ts
|
@cp LICENSE-APACHE $(ROOT)/LICENSE-APACHE
|
||||||
|
@sed -i 's|// @ts-nocheck||' $(ROOT)/index.d.ts
|
||||||
@node -e "\
|
@node -e "\
|
||||||
const pkg = {\
|
const pkg = {\
|
||||||
name: 'blake3-wasm-rs',\
|
name: 'blake3-wasm-rs',\
|
||||||
type: 'module',\
|
type: 'module',\
|
||||||
version: '$(VERSION)',\
|
version: '$(VERSION)',\
|
||||||
description: 'BLAKE3 hashing via Rust/WASM - works in Node.js (CJS + ESM), browsers, and bundlers',\
|
description: 'BLAKE3 hashing via Rust/WASM - works in Node.js (CJS + ESM), browsers, and bundlers',\
|
||||||
license: 'MIT',\
|
license: 'MIT OR Apache-2.0',\
|
||||||
repository: { type: 'git', url: 'https://github.com/UneBaguette/blake3.wasm' },\
|
repository: { type: 'git', url: 'https://github.com/UneBaguette/blake3.wasm' },\
|
||||||
main: 'index.js',\
|
main: 'index.js',\
|
||||||
types: 'index.d.ts',\
|
types: 'index.d.ts',\
|
||||||
exports: { '.': {\
|
exports: { '.': {\
|
||||||
|
types: './index.d.ts',\
|
||||||
node: { require: './node/$(CRATE).js', import: './node-esm/index.mjs' },\
|
node: { require: './node/$(CRATE).js', import: './node-esm/index.mjs' },\
|
||||||
import: './bundler/$(CRATE).js',\
|
webpack: './bundler/$(CRATE).js',\
|
||||||
|
import: './web/$(CRATE).js',\
|
||||||
default: './web/$(CRATE).js'\
|
default: './web/$(CRATE).js'\
|
||||||
}},\
|
}},\
|
||||||
files: ['bundler/', 'web/', 'node/', 'node-esm/', 'index.js', 'index.d.ts', '$(CRATE)_bg.wasm', 'README.md', 'LICENSE'],\
|
files: ['bundler/', 'web/', 'node/', 'node-esm/', 'index.js', 'index.d.ts', '$(CRATE)_bg.wasm', '$(CRATE)_bg.js', 'README.md', 'LICENSE'],\
|
||||||
keywords: ['blake3', 'wasm', 'hash', 'cryptography', 'wasm-bindgen']\
|
keywords: ['blake3', 'wasm', 'hash', 'cryptography', 'wasm-bindgen']\
|
||||||
};\
|
};\
|
||||||
require('fs').writeFileSync('./$(ROOT)/package.json', JSON.stringify(pkg, null, 2) + '\n');"
|
require('fs').writeFileSync('./$(ROOT)/package.json', JSON.stringify(pkg, null, 2) + '\n');"
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ blake3.deriveKey('my context', key);
|
|||||||
|
|
||||||
// Conctruct for Streaming
|
// Conctruct for Streaming
|
||||||
{
|
{
|
||||||
using h = new blake3.Hasher();
|
const h = new blake3.Hasher();
|
||||||
h.update(data.slice(0, 5));
|
h.update(data.slice(0, 5));
|
||||||
h.update(data.slice(5));
|
h.update(data.slice(5));
|
||||||
h.finalize();
|
h.finalize();
|
||||||
@@ -45,23 +45,19 @@ blake3.deriveKey('my context', key);
|
|||||||
|
|
||||||
// Streaming
|
// Streaming
|
||||||
// Keyed (MAC mode)
|
// Keyed (MAC mode)
|
||||||
{
|
const mac = blake3.Hasher.newKeyed(key);
|
||||||
using mac = blake3.Hasher.newKeyed(key);
|
mac.update(data);
|
||||||
mac.update(data);
|
mac.finalize();
|
||||||
mac.finalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Streaming
|
// Streaming
|
||||||
// Key derivation mode
|
// Key derivation mode
|
||||||
{
|
const kdf = blake3.Hasher.newDeriveKey('my app v1 :: subkey');
|
||||||
using kdf = blake3.Hasher.newDeriveKey('my app v1 :: subkey');
|
kdf.update(key);
|
||||||
kdf.update(key);
|
kdf.finalize();
|
||||||
kdf.finalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Batch hashing without re-allocating
|
// Batch hashing without re-allocating
|
||||||
{
|
{
|
||||||
using h = new blake3.Hasher();
|
const h = new blake3.Hasher();
|
||||||
h.update(chunk1);
|
h.update(chunk1);
|
||||||
const first = h.finalizeAndReset();
|
const first = h.finalizeAndReset();
|
||||||
h.update(chunk2);
|
h.update(chunk2);
|
||||||
@@ -73,48 +69,6 @@ blake3.deriveKey('my context', key);
|
|||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { hash, hashXof, keyedHash, deriveKey, Hasher } from 'blake3-wasm-rs';
|
import { hash, hashXof, keyedHash, deriveKey, Hasher } from 'blake3-wasm-rs';
|
||||||
|
|
||||||
const data = new TextEncoder().encode('hello world');
|
|
||||||
const key = new Uint8Array(32).fill(1);
|
|
||||||
|
|
||||||
// One-shot hashing
|
|
||||||
hash(data);
|
|
||||||
hashXof(data, 64); // variable output length
|
|
||||||
keyedHash(data, key); // key must be exactly 32 bytes
|
|
||||||
deriveKey('my context', key);
|
|
||||||
|
|
||||||
// Streaming
|
|
||||||
{
|
|
||||||
using h = new Hasher();
|
|
||||||
h.update(data.slice(0, 5));
|
|
||||||
h.update(data.slice(5));
|
|
||||||
h.finalize();
|
|
||||||
h.finalizeXof(64);
|
|
||||||
h.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keyed (MAC mode)
|
|
||||||
{
|
|
||||||
using mac = Hasher.newKeyed(key);
|
|
||||||
mac.update(data);
|
|
||||||
mac.finalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Key derivation mode
|
|
||||||
{
|
|
||||||
using kdf = Hasher.newDeriveKey('my app v1 :: subkey');
|
|
||||||
kdf.update(key);
|
|
||||||
kdf.finalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Batch hashing without re-allocating
|
|
||||||
{
|
|
||||||
using h = new Hasher();
|
|
||||||
h.update(chunk1);
|
|
||||||
const first = h.finalizeAndReset();
|
|
||||||
h.update(chunk2);
|
|
||||||
const second = h.finalizeAndReset();
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|||||||
Vendored
+4
-1
@@ -1,3 +1,6 @@
|
|||||||
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
// Copyright 2026 Thomas <[email protected]>
|
||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
|
|
||||||
export { Hasher, deriveKey, hash, hashXof, keyedHash } from './bundler/blake3_wasm_rs';
|
export { Hasher, deriveKey, hash, hashXof, keyedHash } from './bundler/blake3_wasm_rs';
|
||||||
|
|
||||||
|
export { default } from './web/blake3_wasm_rs';
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
// Copyright 2026 Thomas <[email protected]>
|
||||||
|
//
|
||||||
// blake3-wasm-rs. Auto-generated entry point
|
// blake3-wasm-rs. Auto-generated entry point
|
||||||
// Do not edit directly!
|
// Do not edit directly!
|
||||||
|
|
||||||
|
|||||||
+199
-57
@@ -1,4 +1,5 @@
|
|||||||
use wasm_bindgen::prelude::*;
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
// Copyright 2026 Thomas <[email protected]>
|
||||||
|
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
not(target_feature = "atomics"),
|
not(target_feature = "atomics"),
|
||||||
@@ -10,77 +11,77 @@ static TALC: talc::wasm::WasmDynamicTalc = talc::wasm::new_wasm_dynamic_allocato
|
|||||||
|
|
||||||
const MAX_XOF_LEN: usize = 1 << 16; // 65_536
|
const MAX_XOF_LEN: usize = 1 << 16; // 65_536
|
||||||
|
|
||||||
fn checked_xof_len(out_len: usize) -> Result<usize, JsError> {
|
#[derive(Debug)]
|
||||||
|
pub struct Blake3Error(pub String);
|
||||||
|
|
||||||
|
impl core::fmt::Display for Blake3Error {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for Blake3Error {}
|
||||||
|
|
||||||
|
fn err(msg: &str) -> Blake3Error {
|
||||||
|
Blake3Error(msg.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn checked_xof_len(out_len: usize) -> Result<usize, Blake3Error> {
|
||||||
if out_len == 0 || out_len > MAX_XOF_LEN {
|
if out_len == 0 || out_len > MAX_XOF_LEN {
|
||||||
return Err(JsError::new("out_len must be between 1 and 65536"));
|
return Err(err("out_len must be between 1 and 65536"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(out_len)
|
Ok(out_len)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hash data and return a 32-byte BLAKE3 digest.
|
/// Hash data and return a 32-byte BLAKE3 digest.
|
||||||
#[wasm_bindgen]
|
pub fn hash(value: &[u8]) -> [u8; 32] {
|
||||||
pub fn hash(value: &[u8]) -> Vec<u8> {
|
*blake3::hash(value).as_bytes()
|
||||||
blake3::hash(value).as_bytes().to_vec()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hash data with variable-length output (XOF mode).
|
/// Hash data with variable-length output (XOF mode).
|
||||||
/// Returns `out_len` bytes of BLAKE3 extended output.
|
/// Returns `out_len` bytes of BLAKE3 extended output.
|
||||||
#[wasm_bindgen(js_name = "hashXof")]
|
pub fn hash_xof(data: &[u8], out_len: usize) -> Result<Vec<u8>, Blake3Error> {
|
||||||
pub fn hash_xof(data: &[u8], out_len: usize) -> Result<Vec<u8>, JsError> {
|
|
||||||
let out_len = checked_xof_len(out_len)?;
|
let out_len = checked_xof_len(out_len)?;
|
||||||
let mut out = vec![0u8; out_len];
|
let mut out = vec![0u8; out_len];
|
||||||
let mut reader = blake3::Hasher::new().update(data).finalize_xof();
|
|
||||||
|
|
||||||
reader.fill(&mut out);
|
blake3::Hasher::new()
|
||||||
|
.update(data)
|
||||||
|
.finalize_xof()
|
||||||
|
.fill(&mut out);
|
||||||
|
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute a keyed BLAKE3 hash (MAC). Key must be exactly 32 bytes.
|
/// Compute a keyed BLAKE3 hash (MAC). Key must be exactly 32 bytes.
|
||||||
/// Throws if the key length is wrong.
|
/// Throws if the key length is wrong.
|
||||||
#[wasm_bindgen(js_name = "keyedHash")]
|
pub fn keyed_hash(data: &[u8], key: &[u8]) -> Result<[u8; 32], Blake3Error> {
|
||||||
pub fn keyed_hash(data: &[u8], key: &[u8]) -> Result<Vec<u8>, JsError> {
|
|
||||||
let key: &[u8; 32] = key
|
let key: &[u8; 32] = key
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| JsError::new("key must be exactly 32 bytes"))?;
|
.map_err(|_| err("key must be exactly 32 bytes"))?;
|
||||||
|
|
||||||
Ok(blake3::keyed_hash(key, data).as_bytes().to_vec())
|
Ok(*blake3::keyed_hash(key, data).as_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Derive a 32-byte key from a context string and key material.
|
/// Derive a 32-byte key from a context string and key material.
|
||||||
/// Context should be a hardcoded, globally unique, application-specific string.
|
/// Context should be a hardcoded, globally unique, application-specific string.
|
||||||
#[wasm_bindgen(js_name = "deriveKey")]
|
pub fn derive_key(context: &str, key_material: &[u8]) -> [u8; 32] {
|
||||||
pub fn derive_key(context: &str, key_material: &[u8]) -> Vec<u8> {
|
blake3::derive_key(context, key_material)
|
||||||
blake3::derive_key(context, key_material).to_vec()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Incremental BLAKE3 hasher for streaming data.
|
|
||||||
///
|
|
||||||
/// ```js
|
|
||||||
/// const hasher = new Hasher();
|
|
||||||
/// hasher.update(chunk1);
|
|
||||||
/// hasher.update(chunk2);
|
|
||||||
/// const digest = hasher.finalize(); // 32 bytes
|
|
||||||
/// ```
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub struct Hasher(blake3::Hasher);
|
pub struct Hasher(blake3::Hasher);
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
impl Hasher {
|
impl Hasher {
|
||||||
/// Create a new hasher for unkeyed hashing.
|
/// Create a new hasher for unkeyed hashing.
|
||||||
#[wasm_bindgen(constructor)]
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Hasher(blake3::Hasher::new())
|
Hasher(blake3::Hasher::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a keyed hasher (MAC mode). Key must be exactly 32 bytes.
|
/// Create a keyed hasher (MAC mode). Key must be exactly 32 bytes.
|
||||||
/// Throws if the key length is wrong.
|
/// Throws if the key length is wrong.
|
||||||
#[wasm_bindgen(js_name = "newKeyed")]
|
pub fn new_keyed(key: &[u8]) -> Result<Hasher, Blake3Error> {
|
||||||
pub fn new_keyed(key: &[u8]) -> Result<Hasher, JsError> {
|
|
||||||
let key: &[u8; 32] = key
|
let key: &[u8; 32] = key
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| JsError::new("key must be exactly 32 bytes"))?;
|
.map_err(|_| err("key must be exactly 32 bytes"))?;
|
||||||
|
|
||||||
Ok(Hasher(blake3::Hasher::new_keyed(key)))
|
Ok(Hasher(blake3::Hasher::new_keyed(key)))
|
||||||
}
|
}
|
||||||
@@ -88,8 +89,7 @@ impl Hasher {
|
|||||||
/// Create a hasher in derive-key mode.
|
/// Create a hasher in derive-key mode.
|
||||||
/// Context should be a hardcoded, globally unique, application-specific string.
|
/// Context should be a hardcoded, globally unique, application-specific string.
|
||||||
/// Feed key material via `update()`, then call `finalize()`.
|
/// Feed key material via `update()`, then call `finalize()`.
|
||||||
#[wasm_bindgen(js_name = "newDeriveKey")]
|
pub fn new_derive_key(context: &str) -> Self {
|
||||||
pub fn new_derive_key(context: &str) -> Hasher {
|
|
||||||
Hasher(blake3::Hasher::new_derive_key(context))
|
Hasher(blake3::Hasher::new_derive_key(context))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,15 +99,15 @@ impl Hasher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return the 32-byte hash digest. Non-destructive. Can be called multiple times.
|
/// Return the 32-byte hash digest. Non-destructive. Can be called multiple times.
|
||||||
pub fn finalize(&self) -> Vec<u8> {
|
pub fn finalize(&self) -> [u8; 32] {
|
||||||
self.0.finalize().as_bytes().to_vec()
|
*self.0.finalize().as_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `out_len` bytes of extended output (XOF mode). Non-destructive.
|
/// Return `out_len` bytes of extended output (XOF mode). Non-destructive.
|
||||||
#[wasm_bindgen(js_name = "finalizeXof")]
|
pub fn finalize_xof(&self, out_len: usize) -> Result<Vec<u8>, Blake3Error> {
|
||||||
pub fn finalize_xof(&self, out_len: usize) -> Result<Vec<u8>, JsError> {
|
|
||||||
let out_len = checked_xof_len(out_len)?;
|
let out_len = checked_xof_len(out_len)?;
|
||||||
let mut out = vec![0u8; out_len];
|
let mut out = vec![0u8; out_len];
|
||||||
|
|
||||||
self.0.finalize_xof().fill(&mut out);
|
self.0.finalize_xof().fill(&mut out);
|
||||||
|
|
||||||
Ok(out)
|
Ok(out)
|
||||||
@@ -115,9 +115,9 @@ impl Hasher {
|
|||||||
|
|
||||||
/// Finalize the hash and reset the hasher in one call.
|
/// Finalize the hash and reset the hasher in one call.
|
||||||
/// Useful for hashing multiple inputs sequentially without creating new instances.
|
/// Useful for hashing multiple inputs sequentially without creating new instances.
|
||||||
#[wasm_bindgen(js_name = "finalizeAndReset")]
|
pub fn finalize_and_reset(&mut self) -> [u8; 32] {
|
||||||
pub fn finalize_and_reset(&mut self) -> Vec<u8> {
|
let out = *self.0.finalize().as_bytes();
|
||||||
let out = self.0.finalize().as_bytes().to_vec();
|
|
||||||
self.0.reset();
|
self.0.reset();
|
||||||
|
|
||||||
out
|
out
|
||||||
@@ -129,17 +129,123 @@ impl Hasher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Hasher {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
mod wasm {
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn hash(value: &[u8]) -> Vec<u8> {
|
||||||
|
super::hash(value).to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen(js_name = "hashXof")]
|
||||||
|
pub fn hash_xof(data: &[u8], out_len: usize) -> Result<Vec<u8>, JsError> {
|
||||||
|
super::hash_xof(data, out_len).map_err(|e| JsError::new(&e.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen(js_name = "keyedHash")]
|
||||||
|
pub fn keyed_hash(data: &[u8], key: &[u8]) -> Result<Vec<u8>, JsError> {
|
||||||
|
super::keyed_hash(data, key)
|
||||||
|
.map(|h| h.to_vec())
|
||||||
|
.map_err(|e| JsError::new(&e.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen(js_name = "deriveKey")]
|
||||||
|
pub fn derive_key(context: &str, key_material: &[u8]) -> Vec<u8> {
|
||||||
|
super::derive_key(context, key_material).to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Incremental BLAKE3 hasher for streaming data.
|
||||||
|
///
|
||||||
|
/// ```js
|
||||||
|
/// const hasher = new Hasher();
|
||||||
|
/// hasher.update(chunk1);
|
||||||
|
/// hasher.update(chunk2);
|
||||||
|
/// const digest = hasher.finalize(); // 32 bytes
|
||||||
|
/// ```
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct Hasher(blake3::Hasher);
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Hasher {
|
||||||
|
/// Create a new hasher for unkeyed hashing.
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Hasher(blake3::Hasher::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a keyed hasher (MAC mode). Key must be exactly 32 bytes.
|
||||||
|
/// Throws if the key length is wrong.
|
||||||
|
#[wasm_bindgen(js_name = "newKeyed")]
|
||||||
|
pub fn new_keyed(key: &[u8]) -> Result<Self, JsError> {
|
||||||
|
let key: &[u8; 32] = key
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| JsError::new("key must be exactly 32 bytes"))?;
|
||||||
|
|
||||||
|
Ok(Hasher(blake3::Hasher::new_keyed(key)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a hasher in derive-key mode.
|
||||||
|
/// Context should be a hardcoded, globally unique, application-specific string.
|
||||||
|
/// Feed key material via `update()`, then call `finalize()`.
|
||||||
|
#[wasm_bindgen(js_name = "newDeriveKey")]
|
||||||
|
pub fn new_derive_key(context: &str) -> Self {
|
||||||
|
Hasher(blake3::Hasher::new_derive_key(context))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Feed data into the hasher. Can be called multiple times for streaming.
|
||||||
|
pub fn update(&mut self, data: &[u8]) {
|
||||||
|
self.0.update(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the 32-byte hash digest. Non-destructive. Can be called multiple times.
|
||||||
|
pub fn finalize(&self) -> Vec<u8> {
|
||||||
|
self.0.finalize().as_bytes().to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return `out_len` bytes of extended output (XOF mode). Non-destructive.
|
||||||
|
#[wasm_bindgen(js_name = "finalizeXof")]
|
||||||
|
pub fn finalize_xof(&self, out_len: usize) -> Result<Vec<u8>, JsError> {
|
||||||
|
let len = super::checked_xof_len(out_len).map_err(|e| JsError::new(&e.0))?;
|
||||||
|
let mut out = vec![0u8; len];
|
||||||
|
|
||||||
|
self.0.finalize_xof().fill(&mut out);
|
||||||
|
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finalize the hash and reset the hasher in one call.
|
||||||
|
/// Useful for hashing multiple inputs sequentially without creating new instances.
|
||||||
|
#[wasm_bindgen(js_name = "finalizeAndReset")]
|
||||||
|
pub fn finalize_and_reset(&mut self) -> Vec<u8> {
|
||||||
|
let out = self.0.finalize().as_bytes().to_vec();
|
||||||
|
|
||||||
|
self.0.reset();
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reset the hasher to its initial state. Preserves the mode (keyed/derive-key).
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
self.0.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hash_deterministic() {
|
fn test_hash_deterministic() {
|
||||||
let a = hash(b"hello");
|
assert_eq!(hash(b"hello"), hash(b"hello"));
|
||||||
let b = hash(b"hello");
|
|
||||||
assert_eq!(a, b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hash_length() {
|
fn test_hash_length() {
|
||||||
assert_eq!(hash(b"hello").len(), 32);
|
assert_eq!(hash(b"hello").len(), 32);
|
||||||
@@ -157,6 +263,11 @@ mod tests {
|
|||||||
assert!(keyed_hash(b"hello", &key).is_ok());
|
assert!(keyed_hash(b"hello", &key).is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_keyed_hash_bad_key() {
|
||||||
|
assert!(keyed_hash(b"hello", &[0u8; 16]).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_derive_key() {
|
fn test_derive_key() {
|
||||||
let a = derive_key("ctx", b"material");
|
let a = derive_key("ctx", b"material");
|
||||||
@@ -174,6 +285,7 @@ mod tests {
|
|||||||
let mut h = Hasher::new();
|
let mut h = Hasher::new();
|
||||||
h.update(b"hello");
|
h.update(b"hello");
|
||||||
h.update(b"world");
|
h.update(b"world");
|
||||||
|
|
||||||
assert_eq!(h.finalize(), oneshot);
|
assert_eq!(h.finalize(), oneshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,6 +294,7 @@ mod tests {
|
|||||||
let oneshot = derive_key("my-ctx", b"material");
|
let oneshot = derive_key("my-ctx", b"material");
|
||||||
let mut h = Hasher::new_derive_key("my-ctx");
|
let mut h = Hasher::new_derive_key("my-ctx");
|
||||||
h.update(b"material");
|
h.update(b"material");
|
||||||
|
|
||||||
assert_eq!(h.finalize(), oneshot);
|
assert_eq!(h.finalize(), oneshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,6 +319,7 @@ mod tests {
|
|||||||
let mut h = Hasher::new_keyed(&key).unwrap();
|
let mut h = Hasher::new_keyed(&key).unwrap();
|
||||||
h.update(b"hello ");
|
h.update(b"hello ");
|
||||||
h.update(b"world");
|
h.update(b"world");
|
||||||
|
|
||||||
assert_eq!(h.finalize(), oneshot);
|
assert_eq!(h.finalize(), oneshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,6 +328,7 @@ mod tests {
|
|||||||
let oneshot = hash_xof(b"hello", 64).unwrap();
|
let oneshot = hash_xof(b"hello", 64).unwrap();
|
||||||
let mut h = Hasher::new();
|
let mut h = Hasher::new();
|
||||||
h.update(b"hello");
|
h.update(b"hello");
|
||||||
|
|
||||||
assert_eq!(h.finalize_xof(64).unwrap(), oneshot);
|
assert_eq!(h.finalize_xof(64).unwrap(), oneshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,6 +337,7 @@ mod tests {
|
|||||||
// First 32 bytes of XOF output should equal the standard hash
|
// First 32 bytes of XOF output should equal the standard hash
|
||||||
let standard = hash(b"test data");
|
let standard = hash(b"test data");
|
||||||
let xof = hash_xof(b"test data", 64).unwrap();
|
let xof = hash_xof(b"test data", 64).unwrap();
|
||||||
|
|
||||||
assert_eq!(&xof[..32], standard.as_slice());
|
assert_eq!(&xof[..32], standard.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,13 +347,30 @@ mod tests {
|
|||||||
h.update(b"junk");
|
h.update(b"junk");
|
||||||
h.reset();
|
h.reset();
|
||||||
h.update(b"hello");
|
h.update(b"hello");
|
||||||
|
|
||||||
assert_eq!(h.finalize(), hash(b"hello"));
|
assert_eq!(h.finalize(), hash(b"hello"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hasher_keyed_bad_key() {
|
||||||
|
assert!(Hasher::new_keyed(&[0u8; 10]).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hash_xof_zero_len() {
|
||||||
|
assert!(hash_xof(b"hello", 0).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hash_xof_over_max() {
|
||||||
|
assert!(hash_xof(b"hello", 65_537).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hasher_keyed() {
|
fn test_hasher_keyed() {
|
||||||
let key = [1u8; 32];
|
let key = [1u8; 32];
|
||||||
let h = Hasher::new_keyed(&key).unwrap();
|
let h = Hasher::new_keyed(&key).unwrap();
|
||||||
|
|
||||||
assert_ne!(h.finalize(), hash(b"").as_slice());
|
assert_ne!(h.finalize(), hash(b"").as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,49 +384,58 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vector_empty() {
|
fn test_vector_empty() {
|
||||||
let out = hash(&[]);
|
|
||||||
let expected =
|
let expected =
|
||||||
hex::decode("af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262")
|
hex::decode("af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(out, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert_eq!(hash(&[]).as_slice(), expected.as_slice());
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vector_1_byte() {
|
fn test_vector_1_byte() {
|
||||||
let input = test_input(1);
|
|
||||||
let out = hash(&input);
|
|
||||||
let expected =
|
let expected =
|
||||||
hex::decode("2d3adedff11b61f14c886e35afa036736dcd87a74d27b5c1510225d0f592e213")
|
hex::decode("2d3adedff11b61f14c886e35afa036736dcd87a74d27b5c1510225d0f592e213")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(out, expected);
|
|
||||||
|
assert_eq!(hash(&test_input(1)).as_slice(), expected.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vector_1025_bytes() {
|
fn test_vector_1025_bytes() {
|
||||||
let input = test_input(1025);
|
|
||||||
let out = hash(&input);
|
|
||||||
let expected =
|
let expected =
|
||||||
hex::decode("d00278ae47eb27b34faecf67b4fe263f82d5412916c1ffd97c8cb7fb814b8444")
|
hex::decode("d00278ae47eb27b34faecf67b4fe263f82d5412916c1ffd97c8cb7fb814b8444")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(out, expected);
|
|
||||||
|
assert_eq!(hash(&test_input(1025)).as_slice(), expected.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vector_keyed_empty() {
|
fn test_vector_keyed_empty() {
|
||||||
let key = b"whats the Elvish word for friend";
|
let key = b"whats the Elvish word for friend";
|
||||||
let out = keyed_hash(&[], key).unwrap();
|
|
||||||
let expected =
|
let expected =
|
||||||
hex::decode("92b2b75604ed3c761f9d6f62392c8a9227ad0ea3f09573e783f1498a4ed60d26")
|
hex::decode("92b2b75604ed3c761f9d6f62392c8a9227ad0ea3f09573e783f1498a4ed60d26")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(out, expected);
|
|
||||||
|
assert_eq!(
|
||||||
|
keyed_hash(&[], key).unwrap().as_slice(),
|
||||||
|
expected.as_slice()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(target_arch = "wasm32", test))]
|
#[cfg(all(target_arch = "wasm32", test))]
|
||||||
mod wasm_tests {
|
mod wasm_tests {
|
||||||
use super::*;
|
use super::wasm::*;
|
||||||
use wasm_bindgen_test::*;
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn test_hash_roundtrip() {
|
||||||
|
let a = hash(b"hello");
|
||||||
|
let b = hash(b"hello");
|
||||||
|
|
||||||
|
assert_eq!(a, b);
|
||||||
|
assert_eq!(a.len(), 32);
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn test_keyed_hash_bad_key() {
|
fn test_keyed_hash_bad_key() {
|
||||||
assert!(keyed_hash(b"hello", &[0u8; 16]).is_err());
|
assert!(keyed_hash(b"hello", &[0u8; 16]).is_err());
|
||||||
|
|||||||
Reference in New Issue
Block a user