aboutsummaryrefslogtreecommitdiff
path: root/sri-hash
blob: 87ba453b9f573bf34ebc0e48277b17972b388394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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