Skip to content

Commit

Permalink
Add WebcHash::try_from_hex helper
Browse files Browse the repository at this point in the history
Closes #4077
  • Loading branch information
theduke committed Jul 18, 2023
1 parent a6c2c86 commit e6c9fe4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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 try_from_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
5 changes: 2 additions & 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,8 @@ 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::try_from_hex(&hash).context("invalid webc sha256 hash in manifest")?;

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

0 comments on commit e6c9fe4

Please sign in to comment.