-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use wasi-crypto as an optionl wasi feature
Signed-off-by: Richard Zak <richard@profian.com>
- Loading branch information
Showing
9 changed files
with
213 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
[env] | ||
RUST_BACKTRACE = "1" | ||
WASMTIME_BACKTRACE_DETAILS = "1" | ||
|
||
[build] | ||
target = "wasm32-wasi" | ||
|
||
[target.wasm32-wasi] | ||
rustflags = ["--cfg", "tokio_unstable"] | ||
runner = ["enarx", "run", "--wasmcfgfile", "Enarx.toml"] | ||
# runner = ["./enarx", "run", "--wasmcfgfile", "Enarx.toml"] | ||
runner = ["/home/rjzak/bin/wasmtime-wasi-crypto", "--wasi-modules", "experimental-wasi-crypto", "--"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 +1,5 @@ | ||
[toolchain] | ||
channel = "stable" | ||
channel = "nightly" | ||
components = [ "rustfmt", "clippy" ] | ||
profile = "minimal" | ||
targets = [ "wasm32-wasi", "x86_64-unknown-linux-musl" ] |
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,51 @@ | ||
// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com> | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use anyhow::Result; | ||
|
||
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))] | ||
use anyhow::anyhow; | ||
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))] | ||
use wasi_crypto_guest::prelude::Hash; | ||
|
||
#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))] | ||
use sha2::{Digest, Sha256, Sha384}; | ||
|
||
#[inline] | ||
pub fn sha256(data: impl AsRef<[u8]>) -> Result<Vec<u8>> { | ||
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))] | ||
return Ok(Hash::hash("SHA-256", data, 32, None).or_else(|_| Err(anyhow!("hash error")))?); | ||
|
||
#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))] | ||
Ok(Sha256::digest(data).as_slice().to_vec()) | ||
} | ||
|
||
#[inline] | ||
pub fn sha384(data: impl AsRef<[u8]>) -> Result<Vec<u8>> { | ||
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))] | ||
return Ok(Hash::hash("SHA-384", data, 48, None).or_else(|_| Err(anyhow!("hash error")))?); | ||
|
||
#[cfg(any(not(target_os = "wasi"), not(feature = "wasi-crypto")))] | ||
Ok(Sha384::digest(data).as_slice().to_vec()) | ||
} | ||
|
||
#[cfg(all(target_os = "wasi", feature = "wasi-crypto"))] | ||
#[cfg(test)] | ||
mod wasi_crypto { | ||
use crate::{sha256, sha384}; | ||
use sha2::Digest; | ||
|
||
const DATA: &[u8] = b"SOME_TEST_DATA"; | ||
|
||
#[test] | ||
fn test_sha256() { | ||
let hash = sha256(DATA).unwrap(); | ||
assert_eq!(hash, sha2::Sha256::digest(DATA).as_slice()); | ||
} | ||
|
||
#[test] | ||
fn test_sha384() { | ||
let hash = sha384(DATA).unwrap(); | ||
assert_eq!(hash, sha2::Sha384::digest(DATA).as_slice()); | ||
} | ||
} |
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
Oops, something went wrong.