]> gitweb.fluxo.info Git - utils-web.git/commitdiff
Feat: sri-hash: support for more hash types, defaults to sha512 and added CSS support main
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 29 Mar 2025 16:39:10 +0000 (13:39 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 29 Mar 2025 16:39:10 +0000 (13:39 -0300)
sri-hash

index 87ba453b9f573bf34ebc0e48277b17972b388394..bb9f5d9fd35ddd64bc3c9da416768530058582ca 100755 (executable)
--- a/sri-hash
+++ b/sri-hash
@@ -9,6 +9,7 @@
 # Parameters
 BASENAME="`basename $0`"
 URI="$1"
+TYPE="${2:-sha512}"
 
 # Check
 if [ -z "$URI" ]; then
@@ -19,12 +20,12 @@ 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`"
+  HASH="`curl $URI | openssl dgst -$TYPE -binary | openssl base64 -A`"
   echo ""
 else
   if [ -e "$URI" ]; then
     # See https://www.srihash.org/
-    HASH="`openssl dgst -sha384 -binary $URI | openssl base64 -A`"
+    HASH="`openssl dgst -$TYPE -binary $URI | openssl base64 -A`"
   else
     echo "file not found: $URI"
     exit 1
@@ -32,6 +33,23 @@ else
 fi
 
 # Generate
-cat <<EOF
-<script src="$URI" integrity="sha384-$HASH" crossorigin="anonymous"></script>
+if echo $URL | grep -q '.js$'; then
+  cat <<EOF
+<script
+  src="$URI"
+  integrity="$TYPE-$HASH"
+  crossorigin="anonymous"
+  referrerpolicy="no-referrer"
+></script>
 EOF
+else
+  cat <<EOF
+<link
+  href="$URI"
+  rel="stylesheet"
+  integrity="$TYPE-$HASH"
+  crossorigin="anonymous"
+  referrerpolicy="no-referrer"
+></script>
+EOF
+fi