/srihash

bash function/script to generate an SRI hash for a remote resource

Primary LanguageShellMIT LicenseMIT

srihash

bash function/script to generate an SRI hash for a remote resource

Install

Not much of an install. Use either the bash function or script version.

Requires openssl.

1. bash function

Add the srihash function to ~/.bash_profile

curl -sL 'https://raw.githubusercontent.com/ping/srihash/master/srihash' >> ~/.bash_profile \
&& source ~/.bash_profile

2. bash script

Download srihash.sh locally.

curl -s -o 'srihash.sh' 'https://raw.githubusercontent.com/ping/srihash/master/srihash.sh' \
&& chmod +x srihash.sh

Usage

srihash 'https://url/to/a.css' 'https://url/to/another.js'
# or
./srihash.sh 'https://url/to/a.css' 'https://url/to/another.js'

# Use sha256 instead
srihash 'sha256' 'https://url/to/a.css' 'https://url/to/another.js'
# or
./srihash.sh 'sha256' 'https://url/to/a.css' 'https://url/to/another.js'

# Use sha512 instead
srihash 'sha512' 'https://url/to/a.css' 'https://url/to/another.js'
# or
./srihash.sh 'sha512' 'https://url/to/a.css' 'https://url/to/another.js'

Example:

srihash 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css' \
'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'
==== Generating hash for: https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css ====
sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
 integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
 crossorigin="anonymous">
==== Generating hash for: https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js ====
sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"
 integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
 crossorigin="anonymous"></script>

The generated <script>/<link> markup will automatically be copied to the clipboard.