skip to Main Content

So,I am learning javascript.And from my understanding script can have an attribute called integrity who value is a hash value.

<script> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" </script>

From my understanding,the integrity attribute is used to check if the external source file is not corrupted or has been changed.

from where will I get the hashvalue that can used as the value of integrity attribute.

2

Answers


  1. Here’s how you can get the integrity hash for a script tag:

    1. Online SRI Hash Generator: You can use an online SRI hash generator to generate integrity hashes12. For example, srihash.org is a commonly used tool.
    2. Command Line: If you prefer to calculate the integrity value locally without sending your files to a third-party website, you can use the following command in bash2:
    openssl dgst -sha384 -binary filename.js | openssl base64 -A
    

    Replace filename.js with the name of your JavaScript file. This command will generate a Base64-encoded cryptographic hash of your file using the SHA-384 algorithm2.

    1. Webpack Plugin: If you’re using Webpack, you can install the html-bundler-webpack-plugin which generates the integrity hashes and adds the integrity attribute to the link and script tags when generating HTML3.

    After generating the hash, you can add it to your script tag like this:

    <script src="filename.js" integrity="sha384-generatedhash" crossorigin="anonymous"></script>
    

    Replace filename.js with the path to your JavaScript file and generatedhash with the hash you generated1.

    Note that the crossorigin attribute should also be included when using the integrity attribute1. This attribute tells the browser to request the script with CORS enabled, which is necessary because the integrity check fails without CORS1.

    Also, side note: Here’s another post where this was answered How to get the integrity value for a jquery version for script

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search