Course contentsShow
← Web Development
Lesson 2938 of 2,98252. Frontend SecurityFree lesson

Generating SRI Hashes

Using tools and commands to generate correct integrity hash values for your external resources.

Generating SRI Hashes

You'll learn how to create integrity hash values for external resources using tools and commands.

Now that you know how to add SRI attributes to your script and link tags, you need the actual hash values. Generating these hashes is straightforward — you feed the resource file through a cryptographic hash function and encode the output in base64.

Core Concept

An SRI hash is just a cryptographic digest of a file's contents. You can generate it using command-line tools, online generators, or build scripts. The most common algorithm is SHA-384, though SHA-256 and SHA-512 also work.

Common Generation Methods

Using OpenSSL (command line):

openssl dgst -sha384 -binary library.js | openssl base64 -A

This pipes the file through SHA-384, outputs binary, then encodes to base64. The result is your hash string.

Using Node.js:

cat library.js | openssl dgst -sha384 -binary | openssl base64 -A

Or with a Node script reading the file and using the crypto module to hash and encode.

Online SRI Hash Generators: Various websites let you paste URLs or upload files to generate hashes instantly — useful for quick testing but less suitable for production workflows.

Why Correct Generation Matters

Even a single byte difference in the file produces a completely different hash. If your generated hash doesn't match the actual resource perfectly, browsers will refuse to load it. Always generate hashes from the exact file version your page will load.

Key Takeaway: Generate SRI hashes using cryptographic tools like OpenSSL or build scripts to create integrity values that browsers can verify against loaded resources.