From d88bed415746787987dc33b7acc014f2cc324c9f Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Sat, 20 Jun 2020 00:51:54 -0600 Subject: [PATCH] initial commit --- .gitattributes | 82 ++++++++++++++++++++++ .gitignore | 10 +++ .vscode/numbered-bookmarks.json | 3 + README.md | 3 + sri.sh | 116 ++++++++++++++++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .vscode/numbered-bookmarks.json create mode 100644 README.md create mode 100755 sri.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94dc80c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,82 @@ +# Common settings that generally should always be used with your language specific settings + +# Auto detect text files and perform LF normalization +# https://www.davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ +* text=auto + +# +# The above will handle all files NOT found below +# + +# Documents +*.bibtex text diff=bibtex +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain +*.md text +*.tex text diff=tex +*.adoc text +*.textile text +*.mustache text +*.csv text +*.tab text +*.tsv text +*.txt text +*.sql text + +# Graphics +*.png binary +*.jpg binary +*.jpeg binary +*.gif binary +*.tif binary +*.tiff binary +*.ico binary +# SVG treated as an asset (binary) by default. +*.svg text +# If you want to treat it as binary, +# use the following line instead. +# *.svg binary +*.eps binary + +# Scripts +*.bash text eol=lf +*.fish text eol=lf +*.sh text eol=lf +# These are explicitly windows files and should use crlf +*.bat text eol=crlf +*.cmd text eol=crlf +*.ps1 text eol=crlf + +# Serialisation +*.json text +*.toml text +*.xml text +*.yaml text +*.yml text + +# Archives +*.7z binary +*.gz binary +*.tar binary +*.tgz binary +*.zip binary + +# Text files where line endings should be preserved +*.patch -text + +# +# Exclude files from exporting +# + +.gitattributes export-ignore +.gitignore export-ignore +README.md export-ignore +vscode export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9302a0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/numbered-bookmarks.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ diff --git a/.vscode/numbered-bookmarks.json b/.vscode/numbered-bookmarks.json new file mode 100644 index 0000000..4c08963 --- /dev/null +++ b/.vscode/numbered-bookmarks.json @@ -0,0 +1,3 @@ +{ + "bookmarks": [] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5cabccb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Sub-Resource Integrity Generator Scripts + +Basic scripts to generate SRI hashs for a given file. POSIX-compliant shell script for use on *nix and PowerShell for use on Windows. diff --git a/sri.sh b/sri.sh new file mode 100755 index 0000000..087aeaa --- /dev/null +++ b/sri.sh @@ -0,0 +1,116 @@ +#!/bin/sh + +# +## generate SRI checksums +# + +### text formatting presets +if command -v tput > /dev/null; then + cyan=$(tput setaf 6) + err=$(tput bold)$(tput setaf 1) + magenta=$(tput setaf 5) + norm=$(tput sgr0) + ok=$(tput setaf 2) +else + cyan='' + err='' + magenta='' + norm='' + ok='' +fi + +### trap +trap trapExit 1 2 3 6 + +### functions +displayError (){ + printf "\n%sERROR: %s\n" "$err" "$2" + printf "Exiting now.%s\n\n" "$norm" + exit "$1" +} + +scriptHelp (){ + printf "\nSeems you need some help?\n\n" + exit 0; +} + +trapExit (){ + printf "\n%sERROR: Caught signal. Exiting.%s\n\n" "$err" "$norm" + exit 99 +} + +### default variables +algo='sha384' +unset filename + +### check pre-requisites +if ! command -v openssl > /dev/null; then + displayError 2 'openSSL is not installed' +fi + +### process startup parameters +if [ -z "$1" ]; then scriptHelp; fi +while [ $# -gt 0 ]; do + case "$1" in + -h|-\?|--help) + # display script help + scriptHelp + exit 0 + ;; + -2|--sha256) + # generate SRI using sha256 + algo='sha256' + ;; + -3|--sha384) + # generate SRI using sha384 (default) + algo='sha384' + ;; + -5|--sha512) + # generate SRI using sha512 + algo='sha512' + ;; + -f|--file) + # file for which to generate SRI hash + if [ -n "$2" ]; then + if [ -f "$2" ]; then + filename="$2" + shift + else + displayError 3 "Cannot find file '${2}'." + fi + else + displayError 3 'No filename specified.' + fi + ;; + *) + # unknown option + printf "\n%sUnknown option: %s.\n" "$err" "$1" + printf "%sUse '--help' for valid options.%s\n\n" "$cyan" "$norm" + exit 1 + ;; + esac + shift +done + +printf "\n%sselected algo: %s%s\n" "$magenta" "$norm" "$algo" +printf "%sselected file: %s%s%s\n\n" "$magenta" "$norm" "$filename" "$norm" + +### do SRI generation +hash=$( openssl dgst -${algo} -binary "${filename}" | openssl base64 -A) > /dev/null 2>&1 +if [ -z "$hash" ]; then + displayError 4 'An error occurred while generating SRI hash.' +else + printf "%sSRI hash: %s%s-%s%s\n\n" "$magenta" "$ok" "$algo" "$hash" "$norm" +fi + +exit 0 + + +### error codes +# 0: no errors, normal execution +# 1: parameter error +# 2: cannot find openSSL binary +# 3: cannot find specified file for which to generate hash +# 4: error occured while executing openssl + +#EOF \ No newline at end of file