Skip to content

Instantly share code, notes, and snippets.

@Douile
Last active July 4, 2020 14:16
Show Gist options
  • Save Douile/44dc412992f31d28c9bb9ed0964623fa to your computer and use it in GitHub Desktop.
Save Douile/44dc412992f31d28c9bb9ed0964623fa to your computer and use it in GitHub Desktop.
A simple bash function to generate a sha512 integrity for any web content
#!/bin/bash
# Generate a HTML5 integrity hash
# Requires openssl and curl
# $1 = URL to fetch
# $2 = Hash method to use (defaults to sha512)
# HTML5 support algorithms: sha256, sha384, sha512
# Example: integrity http://localhost:5000/src/index.js sha256
# Example: integrity http://localhost:5000/src/index.min.js
function integrity {
hash="$2";
if [ -z "$hash" ]; then
hash="sha512";
fi
printf "integrity=\"%s-%s\"\n" "$hash" "$( curl -sS $1 | openssl dgst -sha512 -binary | openssl base64 -A )";
}
[[ $_ != $0 ]] && echo "Loaded integrity..." || integrity $1 $2;

Example usage

# Install dependencies (arch only)
sudo pacman -Ss openssl curl
# Download script
curl "https://gist.githubusercontent.com/Douile/44dc412992f31d28c9bb9ed0964623fa/raw/integrity.sh" -o "integrity.sh" && chmod u+x integrity.sh && source integrity.sh
# Run function
integrity https://github.com/Douile sha512
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment