Before you load a model, compute its SHA-256 and compare it to a hash the attacker can't also edit. Check again at load time, not just at download. And don't read "the hash matches" as "the file is safe." A hash proves you have the file you expected. It says nothing about whether that file was malicious to begin with.
In most AI deployments I've reviewed, the process is download, load, ship. Nobody checks whether the bytes on disk are the bytes the publisher meant to release. That check is cheap, and skipping it leaves a gap attackers already know how to use.
What a hash tells you
A hash function turns a file of any size into a short fingerprint. SHA-256 produces 256 bits, written as 64 hex characters. Change one byte and the result is completely different, so if your hash matches the published one, you have the same bytes that were hashed.
On Linux:
sha256sum model.safetensorsOn macOS, use shasum, which ships with the OS:
shasum -a 256 model.safetensorsOn Windows, either of these works:
certutil -hashfile model.safetensors SHA256
Get-FileHash model.safetensors -Algorithm SHA256The result looks like this:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 model.safetensorsThat particular value is the SHA-256 of an empty file. If you ever see it for a model, the download failed.
Each tool formats its output a little differently. Compare the 64 characters and ignore case. Don't compare whole lines.
Why model files are a special risk
A lot of model files are code in disguise. Python's pickle format, which PyTorch's torch.save uses by default, can run arbitrary code while it loads. The Python docs say so directly. Load a malicious .pt or .pkl file and the attacker's code runs with your permissions.
This has already happened. In early 2024, JFrog's research team reported about 100 malicious models on Hugging Face, some of which gave the attacker a persistent backdoor on whatever machine loaded them. In February 2025, ReversingLabs found two more that used deliberately broken pickle files to get past Picklescan, the scanner Hugging Face uses. Even PyTorch's safer loading mode has had a hole. CVE-2025-32434 allowed code execution through torch.load with weights_only=True in PyTorch 2.5.1 and earlier, and was fixed in 2.6.0.
This is where people overestimate hashing. Every one of those models would have passed a hash check, because the hash published next to each one was the hash of the malicious file. Hashing catches a file that changed after it was published: a tampered mirror, a poisoned cache, a file overwritten on your own storage. It can't catch a file that was bad from the start. That takes the other controls further down.
Where the known-good hash comes from
A hash check is only as strong as the reference value. If an attacker can change the model and its hash in the same place, the check proves nothing.
Hugging Face shows the SHA-256 of every LFS-stored file on its file page. That's good for catching corruption or tampering between Hugging Face and you. It won't help if the repo itself is compromised, because then the attacker controls the file and the page.
The strongest option is a signature, not a bare hash. The OpenSSF model-signing project reached v1.0 in April 2025 and signs models with Sigstore, so you can check both that the files are unchanged and who signed them. Next best is a hash you record yourself when you review and approve a model, kept in a system your model storage can't write to. A hash the publisher posts somewhere other than the download location is better than nothing.
A workflow that holds up
Pin a specific revision. On Hugging Face that means a commit hash, not main, which can change under you. Review it and record the SHA-256 of every file you'll load. Use safetensors files when a repo offers them. The format stores only tensor data and has no way to run code when it loads.
Store those hashes away from the models: a Git repo with protected branches, or a registry with tighter access than your model bucket. If one stolen credential can rewrite both the model and its hash, you don't have a control.
Check again every time you load, not just at download. The download check catches a bad transfer. The load check catches the file someone swapped on disk last week. On big files this costs real time. In a quick test, sha256sum hashed 2 GB in about 1.3 seconds, so a 15 GB checkpoint takes around ten seconds. That's a startup cost, and it's worth paying.
If the hash doesn't match, don't load the file. Raise an alert and treat it as a possible incident until you know why.
Here's a minimal check you can put in front of your loader:
import hashlib
import hmac
import sys
def sha256_file(path, chunk_size=1024 * 1024):
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(chunk_size), b""):
h.update(chunk)
return h.hexdigest()
def verify_or_die(path, expected):
actual = sha256_file(path)
if not hmac.compare_digest(actual, expected.strip().lower()):
sys.exit(f"Hash mismatch for {path}: expected {expected}, got {actual}. Not loading.")
return path
# model_path = verify_or_die("model.safetensors", APPROVED["model.safetensors"])It reads the file in 1 MB chunks, so a 20 GB checkpoint never has to fit in memory.
Keep an approved model list
Write down which models your organization is allowed to run. For each one, record the name, the exact revision, where it came from, the SHA-256 of each file, who approved it and when. A YAML file in a protected repo is enough to start:
- name: example-org/example-model
revision: <commit hash>
source: huggingface
files:
model.safetensors: <sha256>
config.json: <sha256>
approved_by: security-team
approved_on: 2026-11-20The rule is simple: if it isn't on the list, it doesn't load in production. That includes "just testing it locally" on a laptop that holds production credentials. The list also gives you an audit trail. When something goes wrong, you can say exactly which model was running and who signed off on it.
Hash more than the weights
A model repo is rarely one file. There are configs, tokenizer files, LoRA adapters, quantized variants and separate embedding models, and an attacker only needs to change one of them. Hash everything your code loads.
Hash your training and fine-tuning datasets too, but know what that buys you. It tells you the data changed. It doesn't tell you the data is clean. If the dataset was poisoned before you hashed it, the hash will faithfully confirm you have the poisoned version.
Where teams slip
The most common slip is checking once at download and never again, so a file swapped later goes unnoticed. Close behind is storing the hash next to the model, where whoever replaces one can replace the other. Approving main instead of a commit means the thing you approved isn't the thing you're running. And the quietest mistake is treating a matching hash as a safety verdict.
What hashing doesn't cover
Hashing won't find a backdoor trained into the weights, a bug in your loading library, or a file that was malicious when it was published. For those, use safetensors wherever you can. If you must load pickle-based PyTorch files, run PyTorch 2.6 or later, where torch.load defaults to weights_only=True. Scan pickle files before loading them (Picklescan, or Trail of Bits' Fickling), and load anything you don't fully trust in a sandbox with no credentials. Where publishers support it, verify signatures instead of bare hashes.
Start this week
Pick one model you run in production and compute the SHA-256 of every file it loads.
Record those hashes, with the exact revision, in a repo or registry your model storage can't write to.
Add a load-time check like the one above, and make it fail closed.
Swap any pickle-based files for safetensors versions where they exist.
Turn your notes into an approved model list and make it the gate for production.


