diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2024-08-20 21:01:32 -0300 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2024-08-20 21:01:32 -0300 |
commit | c3a1bf60e9694b6ac725016a15f19f49905e541f (patch) | |
tree | a35339e755a19e525342861d7fad5ec4e3e99db0 /sri-hash | |
download | utils-web-c3a1bf60e9694b6ac725016a15f19f49905e541f.tar.gz utils-web-c3a1bf60e9694b6ac725016a15f19f49905e541f.tar.bz2 |
Initial import
Diffstat (limited to 'sri-hash')
-rwxr-xr-x | sri-hash | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/sri-hash b/sri-hash new file mode 100755 index 0000000..87ba453 --- /dev/null +++ b/sri-hash @@ -0,0 +1,37 @@ +#!/bin/sh +# +# SRI Hash Generator +# +# A safer alternative than using external services such as +# https://www.srihash.org +# + +# Parameters +BASENAME="`basename $0`" +URI="$1" + +# Check +if [ -z "$URI" ]; then + echo "usage: $BASENAME <file-or-url>" + exit 1 +fi + +# Get file +if echo "$URI" | grep -q '^http'; then + echo "downloading $URI and generating hash..." + HASH="`curl $URI | openssl dgst -sha384 -binary | openssl base64 -A`" + echo "" +else + if [ -e "$URI" ]; then + # See https://www.srihash.org/ + HASH="`openssl dgst -sha384 -binary $URI | openssl base64 -A`" + else + echo "file not found: $URI" + exit 1 + fi +fi + +# Generate +cat <<EOF +<script src="$URI" integrity="sha384-$HASH" crossorigin="anonymous"></script> +EOF |