Safeguard
Supply Chain•

How do you check a mirrored npm package against the lockfile hash?

The integrity field in package-lock.json records a checksum for each package. How to recompute it for a tarball from your mirror and compare, step by step.

Safeguard Security Team
3 min read

Download the tarball from your mirror, compute its sha512 digest, encode it in base64, and compare it with the integrity value in package-lock.json for that package. If they match, the mirror served the same bytes your lockfile recorded. If they differ, stop, because either the file changed or the lockfile entry did.

What the field means

The lockfile documentation says the integrity field is a checksum for the downloaded artifact that npm unpacked at that location, in the standard subresource integrity format, using sha512 or sha1. npm uses it to confirm that content matches what was recorded when the entry was written. Normal installs do this automatically, so a manual check is for investigations and for building trust in a new mirror.

Prefer entries with sha512. The sha1 form exists for older entries and is weaker.

A manual check

  1. Find the package entry in package-lock.json and copy its resolved URL and integrity value.
  2. Download the tarball from your mirror with a tool that does not alter it.
  3. Compute the digest and compare.
curl -sSLO https://mirror.example.internal/lodash/-/lodash-4.17.21.tgz
openssl dgst -sha512 -binary lodash-4.17.21.tgz | openssl base64 -A
grep -A2 '"node_modules/lodash"' package-lock.json | grep integrity

The integrity value begins with the algorithm name and a dash, then the base64 digest. Compare only the digest part. The version in the example is illustrative, so substitute a package from your own lockfile.

Make the comparison meaningful

A match tells you the mirror's copy equals what the lockfile recorded. It does not say that the recorded file was good. Consider where the lockfile entry came from. If the entry was created while pointed at a poisoned source, the hash faithfully records the poison. So also compare against upstream.

ComparisonWhat a match proves
Mirror copy versus lockfileThe mirror serves what you recorded
Mirror copy versus upstream copyThe mirror did not alter the file
Lockfile entry versus a clean past commitThe entry was not changed in a recent edit

For the second row, download the same version straight from the public registry and compare digests. Equal digests mean the mirror is faithful for that file.

When to run it

Run it when you change mirror software, when an advisory mentions registry tampering, or when a lockfile diff changes an integrity value without a version change. That last case deserves a question in code review, because a legitimate update changes the version too.

Review lockfile diffs

Integrity changes in a pull request are easy to skim past. Ask for a short note whenever a hash changes alone. A related post covers lockfile integrity in the audit workflow.

Where Safeguard fits

Safeguard's pull request gates and SBOM scanning can surface dependency changes for reviewers. A free scan is at gold.safeguard.sh/scan.

Try it yourself

Click through it at your own pace, or open it full screen.

See how a gate presents a dependency change for review, then picture an integrity-only change appearing the same way.

Quick check

Does npm already do this on every install?

Yes, it verifies content against the recorded integrity. The manual check helps investigations and mirror trust.

What if the hashes differ?

Do not install. Compare with upstream, check recent lockfile commits and tell the mirror owner.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.