Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Crypto HmacSha256 support #696

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ docs:
cargo doc --package=javy-core --open --target=wasm32-wasi

test-javy:
CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=." cargo wasi test --package=javy --features json,messagepack -- --nocapture
CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=." cargo wasi test --package=javy --features json,messagepack,crypto -- --nocapture

test-core:
cargo wasi test --package=javy-core -- --nocapture
Expand All @@ -37,7 +37,7 @@ test-runner:
cargo test --package=javy-runner -- --nocapture

# WPT requires a Javy build with the experimental_event_loop feature to pass
test-wpt: export CORE_FEATURES ?= experimental_event_loop
test-wpt: export CORE_FEATURES ?= experimental_event_loop,crypto
test-wpt:
# Can't use a prerequisite here b/c a prequisite will not cause a rebuild of the CLI
$(MAKE) cli
Expand Down
2 changes: 2 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bitflags! {
const JAVY_STREAM_IO = 1 << 2;
const REDIRECT_STDOUT_TO_STDERR = 1 << 3;
const TEXT_ENCODING = 1 << 4;
const CRYPTO = 1 << 5;
}
}

Expand All @@ -44,5 +45,6 @@ mod tests {
assert!(Config::JAVY_STREAM_IO == Config::from_bits(1 << 2).unwrap());
assert!(Config::REDIRECT_STDOUT_TO_STDERR == Config::from_bits(1 << 3).unwrap());
assert!(Config::TEXT_ENCODING == Config::from_bits(1 << 4).unwrap());
assert!(Config::CRYPTO == Config::from_bits(1 << 5).unwrap());
}
}
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ javy-config = { workspace = true }

[features]
experimental_event_loop = []
crypto = ["experimental_event_loop", "javy/crypto"]
7 changes: 5 additions & 2 deletions crates/core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use javy_config::Config as SharedConfig;

pub(crate) fn new(shared_config: SharedConfig) -> Result<Runtime> {
let mut config = Config::default();
let config = config
config
.text_encoding(shared_config.contains(SharedConfig::TEXT_ENCODING))
.redirect_stdout_to_stderr(shared_config.contains(SharedConfig::REDIRECT_STDOUT_TO_STDERR))
.javy_stream_io(shared_config.contains(SharedConfig::JAVY_STREAM_IO))
Expand All @@ -14,5 +14,8 @@ pub(crate) fn new(shared_config: SharedConfig) -> Result<Runtime> {
.override_json_parse_and_stringify(false)
.javy_json(false);

Runtime::new(std::mem::take(config))
#[cfg(feature = "crypto")]
config.crypto(shared_config.contains(SharedConfig::CRYPTO));

Runtime::new(std::mem::take(&mut config))
}
5 changes: 5 additions & 0 deletions crates/javy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["wasm"]
anyhow = { workspace = true }
rquickjs = { version = "=0.6.1", features = ["array-buffer", "bindgen"] }
rquickjs-core = "=0.6.1"
rquickjs-macro = "=0.6.1"
rquickjs-sys = "=0.6.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand All @@ -24,6 +25,8 @@ quickcheck = "1"
bitflags = { workspace = true }
fastrand = "2.1.0"
simd-json = { version = "0.13.10", optional = true, default-features = false, features = ["big-int-as-float", "serde_impl"] }
sha2 = { version = "0.10.8", optional = true }
hmac = { version = "0.12.1", optional = true }

[dev-dependencies]
javy-test-macros = { path = "../test-macros/" }
Expand All @@ -39,3 +42,5 @@ messagepack = ["rmp-serde", "serde-transcode"]
# implications of enabling by default (due to the extra dependencies) and also
# because the native implementation is probably fine for most use-cases.
json = ["serde_json", "serde-transcode", "simd-json"]
# Enable support for WinterCG-compatible Crypto APIs
crypto = ["dep:sha2", "dep:hmac"]
17 changes: 17 additions & 0 deletions crates/javy/src/apis/crypto/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(function() {
const __javy_cryptoSubtleSign = globalThis.__javy_cryptoSubtleSign;

const crypto = {
subtle: {}
};


crypto.subtle.sign = function(obj, key, msg) {
return new Promise((resolve, _) => {
resolve(__javy_cryptoSubtleSign(obj, key, msg));
});
}

globalThis.crypto = crypto;
// Reflect.deleteProperty(globalThis, "__javy_cryptoSubtleSign");
})();
Loading
Loading