Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions libs/npm_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ parking_lot.workspace = true
percent-encoding.workspace = true
serde.workspace = true
serde_json.workspace = true
sha1.workspace = true
sha2.workspace = true
sys_traits.workspace = true
tar.workspace = true
thiserror.workspace = true
Expand All @@ -41,9 +39,12 @@ url.workspace = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
deno_error = { workspace = true, features = ["serde", "serde_json", "tokio"] }
deno_unsync = { workspace = true, features = ["tokio"] }
aws-lc-rs.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
flate2 = { workspace = true, features = ["rust_backend"] }
sha2.workspace = true
sha1.workspace = true

[dev-dependencies]
sys_traits = { workspace = true, features = ["real"] }
Expand Down
32 changes: 28 additions & 4 deletions libs/npm_cache/tarball_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use deno_npm::registry::NpmPackageVersionDistInfo;
use deno_npm::registry::NpmPackageVersionDistInfoIntegrity;
use deno_semver::package::PackageNv;
use flate2::read::GzDecoder;
use sha2::Digest;
use sys_traits::FsCanonicalize;
use sys_traits::FsCreateDirAll;
use sys_traits::FsFileSetPermissions;
Expand All @@ -26,6 +25,31 @@ use sys_traits::ThreadSleep;
use tar::Archive;
use tar::EntryType;

#[cfg(target_arch = "wasm32")]
mod hashing {
use sha2::Digest;

pub fn sha1(data: &[u8]) -> impl AsRef<[u8]> {
sha1::Sha1::digest(data)
}

pub fn sha512(data: &[u8]) -> impl AsRef<[u8]> {
sha2::Sha512::digest(data)
}
}

#[cfg(not(target_arch = "wasm32"))]
mod hashing {
use aws_lc_rs::digest;
pub fn sha1(data: &[u8]) -> impl AsRef<[u8]> {
digest::digest(&digest::SHA1_FOR_LEGACY_USE_ONLY, data)
}

pub fn sha512(data: &[u8]) -> impl AsRef<[u8]> {
digest::digest(&digest::SHA512, data)
}
}

#[derive(Debug, Copy, Clone)]
pub enum TarballExtractionMode {
/// Overwrites the destination directory without deleting any files.
Expand Down Expand Up @@ -161,8 +185,8 @@ fn verify_tarball_integrity(
base64_hash,
} => {
let tarball_checksum = match *algorithm {
"sha512" => BASE64_STANDARD.encode(sha2::Sha512::digest(data)),
"sha1" => BASE64_STANDARD.encode(sha1::Sha1::digest(data)),
"sha512" => BASE64_STANDARD.encode(hashing::sha512(data)),
"sha1" => BASE64_STANDARD.encode(hashing::sha1(data)),
hash_kind => {
return Err(TarballIntegrityError::NotImplementedHashFunction {
package: Box::new(package.clone()),
Expand All @@ -173,7 +197,7 @@ fn verify_tarball_integrity(
(tarball_checksum, base64_hash)
}
NpmPackageVersionDistInfoIntegrity::LegacySha1Hex(hex) => {
let digest = sha1::Sha1::digest(data);
let digest = hashing::sha1(data);
let tarball_checksum = faster_hex::hex_string(digest.as_ref());
(tarball_checksum, hex)
}
Expand Down