How to Generate a Subresource Integrity (SRI) Hash for a Script in the Terminal
A <script> tag that loads code from another site, such as a CDN, runs whatever that site sends. An integrity attribute with the file's hash makes the browser check the file first, and refuse to run it if even one character changed. You make the hash with one command in the terminal.
Syntax
The attribute, on the script tag:
<script src="<file>"
integrity="sha384-<hash>"
crossorigin="anonymous"></script>
The command that makes its value, on macOS and Linux:
echo "sha384-$(openssl dgst -sha384 -binary <file> | openssl base64 -A)"
And in PowerShell 7.6:
$bytes = Get-Content -Path <file> -AsByteStream -Raw
"sha384-" + [Convert]::ToBase64String([System.Security.Cryptography.SHA384]::HashData($bytes))
| Part | What it does |
|---|---|
integrity="sha384-<hash>" | The hash the file must have: the algorithm, a dash, then the hash in base64 |
crossorigin="anonymous" | Fetches the file with CORS, which the browser needs before it can check a file from another site |
openssl dgst -sha384 -binary <file> | Makes the file's SHA-384 hash as raw bytes |
openssl base64 -A | Turns those bytes into base64, on one line |
echo "sha384-$(...)" | Puts the sha384- prefix in front, so the output is the whole attribute value |
SHA384.HashData and ToBase64String | The same two steps in PowerShell, with .NET |
MDN's Subresource Integrity guide describes the feature: Subresource Integrity (SRI) is a security feature that enables browsers to verify that resources they fetch (for example, from a CDN) are delivered without unexpected manipulation. It works by allowing you to provide a cryptographic hash that a fetched resource must match. The allowed prefixes are sha256, sha384 and sha512. sha384 is the one MDN's examples use.
The Example
A page, index.html, says The script hasn't run. until its script, greeting.js, replaces the text with Hello from greeting.js! The guide makes the script's hash, puts it on the script tag, changes the script, and watches the browser refuse it until the hash is made again.
Prerequisites
- macOS or Linux: OpenSSL. Check with
openssl version. This guide used OpenSSL 4.1.0. The LibreSSLopensslthat comes with macOS gave the same hash. - Windows: PowerShell 7.6, the current long-term support release. Windows PowerShell 5.1 has neither
Get-Content -AsByteStreamnorSHA384.HashData. - Python 3, for a small local web server. Check with
python3 --version, orpy --versionon Windows. This guide used Python 3.15.2.
Step 1: Write the Script
Create a folder for the example, and in it a file named greeting.js:
document.getElementById("greeting").textContent = "Hello from greeting.js!";
It finds the element with the ID greeting and replaces its text.
Step 2: Make Its Hash
In the same folder:
- macOS / Linux
- Windows (PowerShell)
echo "sha384-$(openssl dgst -sha384 -binary greeting.js | openssl base64 -A)"
sha384-uh0CzMFTgfAeH+YhrLE0flpqW+lsfjY0F9neG2jdFQo75Bb8wepYwQ+7EF8x8JjY

-binary hands the raw hash to openssl base64. Without it, openssl dgst prints the hash as hexadecimal text, and base64 of that text is a different, wrong value.
$bytes = Get-Content -Path greeting.js -AsByteStream -Raw
"sha384-" + [Convert]::ToBase64String([System.Security.Cryptography.SHA384]::HashData($bytes))
sha384-uh0CzMFTgfAeH+YhrLE0flpqW+lsfjY0F9neG2jdFQo75Bb8wepYwQ+7EF8x8JjY

Get-Content -AsByteStream -Raw reads the file as bytes, exactly as they are on disk, from the folder PowerShell is in.
Both give the same value, because the hash depends only on the file's bytes. Your value will differ if your file differs in any way, even by one space or a different line ending.
Step 3: Put the Hash on the Script Tag
Create index.html next to greeting.js, with the value from Step 2:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SRI demo</title>
</head>
<body>
<p id="greeting">The script hasn't run.</p>
<script src="greeting.js"
integrity="sha384-uh0CzMFTgfAeH+YhrLE0flpqW+lsfjY0F9neG2jdFQo75Bb8wepYwQ+7EF8x8JjY"
crossorigin="anonymous"></script>
</body>
</html>
Step 4: Open the Page
The page has to come from a web server: opened straight from the disk, the browser blocks the script for another reason, as Fixed: Access to script at 'file:///…' from origin 'null' has been blocked by CORS policy shows. Python has a small one built in. In the folder:
- macOS / Linux
- Windows (PowerShell)
python3 -m http.server 8081
py -m http.server 8081
Serving HTTP on :: port 8081 (http://[::]:8081/) ...
It serves the folder's files at http://localhost:8081/ until you press Ctrl+C. Open that address in a browser:

The browser made its own hash of greeting.js, found it equal to the one on the tag, and ran the script.
Step 5: Change the Script
Now change the text in greeting.js, as an update to the file would:
document.getElementById("greeting").textContent = "Hello from the new greeting.js!";
Reload the page. The text stays as it was, because the browser didn't run the script:

The browser's console, which F12 opens in Chrome and Edge, says why:
Failed to find a valid digest in the 'integrity' attribute for resource 'http://localhost:8081/greeting.js' with computed SHA-384 integrity 'IVgd0lDQl4XfieCfj3m+tlhOPZteADgAx/Sn3z5gsb8AZaPQR+Gf/lI6vqmG51R0'. The resource has been blocked.
This is SRI doing its job. MDN describes the check: if the actual value matches any of the specified values, then the browser will load the resource, otherwise it will refuse to load the resource, and return a network error. If someone had changed a file on a CDN, your page would refuse it the same way.
Step 6: Make a New Hash for the New File
When the change is one you trust, make the hash again, exactly as in Step 2:
sha384-IVgd0lDQl4XfieCfj3m+tlhOPZteADgAx/Sn3z5gsb8AZaPQR+Gf/lI6vqmG51R0
Replace the old value in index.html's integrity attribute with it, and reload:

The console's message showed the same hash. Make it yourself anyway, from a file you trust: copying it from the error would approve whatever the server sent.
Stop the web server with Ctrl+C when you're done.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out -binary | The hash is of the hexadecimal text, and the browser blocks the script | Keep -binary before the file name |
Leaving out the sha384- prefix | The console says Error parsing 'integrity' attribute, and the browser runs the script without checking it | Start the value with sha384- |
| Hashing a copy that differs from what the site serves | The browser blocks the script | Hash the exact file at the exact URL, such as a CDN file downloaded with curl -o |
Leaving out crossorigin="anonymous" on a script from another site | Blocked: the resource requires the request to be CORS enabled to check the integrity, and it is not | Keep crossorigin="anonymous" |
| Using a CDN address that changes, such as a latest version | The file changes and the page breaks | Load an exact version, and hash that file |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Failed to find a valid digest in the 'integrity' attribute | The file and the hash don't match | Make the hash again from the file the page loads |
No 'Access-Control-Allow-Origin' header is present on the requested resource | The other site doesn't allow CORS for that file | Use a CDN that does, or host the file yourself |
Access to script at 'file:///…' from origin 'null' has been blocked by CORS policy | The page was opened from the disk | Serve the folder, as in Step 4 |
openssl: command not found | OpenSSL isn't installed or isn't on the PATH | Install it, or use the PowerShell tab |
FAQ
Does SRI work for CSS files too?
Yes. The same integrity and crossorigin attributes go on a <link rel="stylesheet"> tag, with the stylesheet's hash.
Why sha384 and not sha256?
Both are allowed, along with sha512, and any of them catches a changed file. sha384 is the one MDN's examples use.
Do I need SRI for my own scripts?
It matters most for files you don't host yourself, because those can change without you knowing. For your own files, you control what's on the server.
Conclusion
An integrity attribute holds a file's hash, and the browser runs the file only if its own hash matches. Make the value with openssl dgst -sha384 -binary and openssl base64 -A on macOS and Linux, or SHA384.HashData in PowerShell 7.6, and put sha384- in front. Add crossorigin="anonymous", and make a new hash whenever you move to a new version of the file.
Additional Resources
- Subresource Integrity, on MDN.
- The integrity attribute, on MDN.
- openssl-dgst, the OpenSSL manual.
