Skip to content

Instantly share code, notes, and snippets.

@gibatronic
Last active February 24, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gibatronic/08c3f1ed0e5e8bc74229 to your computer and use it in GitHub Desktop.
Save gibatronic/08c3f1ed0e5e8bc74229 to your computer and use it in GitHub Desktop.

integrity

Simple tool to generate the integrity attribute for subresource integrity.

Example

Let's say you want to use Backbone from this CDN:

https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js

Just pass that address to the tool:

./integrity https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js

And then use the output tu build the script tag:

<script crossorigin="anonymous" integrity="sha384-kgH1F06klaG52/uQEQlpP5QZ9tbJZgcU4omvs1DRSHaJGVZWp//NYtoi93ZmGday" src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js"></script>

Voilà! You're now safe from unexpected manipulation. The resource you asked for is going to be the one you wanted.

#!/usr/bin/env bash
main() {
local URL=$1
if [ -z "$URL" ]; then
echo 'No URL was given.' 1>&2
return 1
fi
local DIGEST=$(
curl -s $URL |
openssl dgst -sha384 -binary |
openssl enc -base64 -A
)
echo "sha384-$DIGEST"
return 0
}
main $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment