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 WebcHash::parse_hex helper #4081

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
7 changes: 7 additions & 0 deletions lib/wasix/src/runtime/resolver/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ impl WebcHash {
WebcHash(bytes)
}

/// Parse a sha256 hash from a hex-encoded string.
pub fn parse_hex(hex_str: &str) -> Result<Self, hex::FromHexError> {
let mut hash = [0_u8; 32];
hex::decode_to_slice(hex_str, &mut hash)?;
Ok(Self(hash))
}

pub fn for_file(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
let mut hasher = Sha256::default();
let mut reader = BufReader::new(File::open(path)?);
Expand Down
4 changes: 1 addition & 3 deletions lib/wasix/src/runtime/resolver/wapm_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ fn decode_summary(pkg_version: WapmWebQueryGetPackageVersion) -> Result<PackageS
let manifest: Manifest = serde_json::from_slice(manifest.as_bytes())
.context("Unable to deserialize the manifest")?;

let mut webc_sha256 = [0_u8; 32];
hex::decode_to_slice(&hash, &mut webc_sha256)?;
let webc_sha256 = WebcHash::from_bytes(webc_sha256);
let webc_sha256 = WebcHash::parse_hex(&hash).context("invalid webc sha256 hash in manifest")?;

Ok(PackageSummary {
pkg: PackageInfo::from_manifest(&manifest)?,
Expand Down