Compare commits

...

5 Commits

Author SHA1 Message Date
Asif Bacchus 395072f2c2 edit readme title 2020-06-20 05:26:25 -06:00
Asif Bacchus 7ad7edae9d update readme 2020-06-20 05:25:53 -06:00
Asif Bacchus 432b250094 update embedded help 2020-06-20 04:58:30 -06:00
Asif Bacchus ed367f7963 add note about copying to path to embedded help 2020-06-20 04:52:22 -06:00
Asif Bacchus 9c66119ae3 update embedded help 2020-06-20 04:48:17 -06:00
2 changed files with 54 additions and 19 deletions

View File

@ -1,28 +1,50 @@
# Sub-Resource Integrity Generator Scripts
# Sub-Resource Integrity Hash Generator Scripts
Basic scripts to generate SRI hashes for a given file. POSIX-compliant shell script for use on *nix and PowerShell for use on Windows.
Basic scripts to generate SRI hashes. POSIX-compliant shell script for use on *nix and PowerShell for use on Windows.
## common features
- Hash individual files or a quoted space-delimited list of files.
- Hash all files within a specified directory with one command.
- Hash a filtered-list of files within a directory with one command.
- Process a list of files and a directory (filtered or not) at the same time, saving you typing!
## linux script
- This script *requires* openssl be installed and will exit if it cannot find openssl.
- You can rename *sri* to anything you like.
- I suggest copying *sri* somewhere like */usr/local/bin* or */usr/bin* so it can be run easier and from anywhere
- Complete help is included in the script. Simply run without any parameters or run with '*--help*'
- I suggest copying *sri* somewhere like */usr/local/bin* or */usr/bin* so it can be run easier and from anywhere (see note below).
- Complete help is included in the script. Simply run without any parameters or run with '*--help*'.
### examples
```bash
./sri --help
```
Assuming you have *not* copied the script to your path and it is located in your home directory:
### copy to path location
Copying the script to a location within your path makes running it more convenient. For example:
Assuming you store it in your home directory /Downloads and need to hash files in your webroot (eg: /var/www/css/...)
```bash
cd ~
./sri -f /var/www/css/style.css
~/SRIhelper/sri -f /var/www/css/style.css
```
If copied to a directory in your path like */usr/local/bin*, then you can simplify things by running it directly from where the file you want to hash is located:
Whereas, if it's in your path, you can omit the source path and just run
```bash
cd /var/www/css
sri -f style.css
sri -f /var/www/css/style.css
```
To make this work, just copy the file to a location in your path. There are no dependencies or anything to worry about, the file is self-contained and POSIX compliant.
```bash
# copy to local/bin
cp ~/SRIhelper/sri /usr/local/bin/sri
# copy and rename to something else
cp ~/SRIhelper/sri /usr/local/bin/hashSRI
# copy to your global bin directory (usually local is preferred!)
cp ~/SRIhelper/sri /usr/bin/sri
```
### troubleshooting

29
sri
View File

@ -30,18 +30,31 @@ displayError (){
}
scriptHelp (){
printf "\n%sUsage: %s%s %s[--help] [--sha256|--sha384|--sha512] --file /file/to/hash%s\n\n" "$magenta" "$norm" "$scriptName" "$cyan" "$norm"
printf "\n%sUsage: %s%s %s[--help] [--sha256|--sha384|--sha512] %s--file '/path/to/file1 %s[/path/to/file2 ...]'%s --directory /directory/to/hash %s[--filter 'filter']%s\n\n" "$magenta" "$norm" "$scriptName" "$cyan" "$norm" "$cyan" "$norm" "$cyan" "$norm"
printf "If both '--file' and '--directory' are specified, *both* will be processed.\n\n"
printf "%s---parameters---%s\n" "$magenta" "$norm"
printf "%s-h|-?|--help%s: show this help page\n" "$cyan" "$norm"
printf "%s-2|--sha256%s: generate SHA256 SRI hash\n" "$cyan" "$norm"
printf "%s-3|--sha384%s: generate SHA384 SRI hash (default)\n" "$cyan" "$norm"
printf "%s-5|--sha512%s: generate SHA512 SRI hash\n" "$cyan" "$norm"
printf "%s-f|--file%s: full path to the file for which you wish the SRI hash generated (required)\n\n" "$cyan" "$norm"
printf "%s-h|-?|--help%s: Show this help page\n" "$cyan" "$norm"
printf "%s-2|--sha256%s: Generate SHA256 SRI hash\n" "$cyan" "$norm"
printf "%s-3|--sha384%s: Generate SHA384 SRI hash (default)\n" "$cyan" "$norm"
printf "%s-5|--sha512%s: Generate SHA512 SRI hash\n" "$cyan" "$norm"
printf "%s-f|--file%s: Full path of the file(s) to hash. Quoted space-delimited list accepted. Wildcards NOT accepted.\n" "$cyan" "$norm"
printf "%s-d|--dir|--directory%s: Hash each file within specified directory.\n" "$cyan" "$norm"
printf "%s--filter%s: Only considered when processing a directory (-d flag). Will only hash files matching this filter. Eg: '*.css', 'file*.ext'\n\n" "$cyan" "$norm"
printf "%s---examples---%s\n" "$magenta" "$norm"
printf "Generate default SHA384 hash for styles.css located in the current directory:\n"
printf "%s%s -f styles.css%s\n\n" "$cyan" "$scriptName" "$norm"
printf "%s./%s -f styles.css%s\n\n" "$cyan" "$scriptName" "$norm"
printf "Generate SHA512 hash for /var/www/js/script.js:\n"
printf "%s%s -5 --file /var/www/js/script.js%s\n\n" "$cyan" "$scriptName" "$norm"
printf "%s./%s -5 --file /var/www/js/script.js%s\n\n" "$cyan" "$scriptName" "$norm"
printf "Generate default SHA384 hashes for 'script.js' and 'style.css', located in different places:\n"
printf "%s./%s -f '/var/www/js/script.js /var/www/css/style.css'%s\n\n" "$cyan" "$scriptName" "$norm"
printf "Generate default SHA384 hashes for all files in ~/webpage/css/:\n"
printf "%s./%s --directory ~/webpage/css%s\n\n" "$cyan" "$scriptName" "$norm"
printf "Generate default SHA384 hashes for all files with names starting with 'foo' within /var/www/includes/:\n"
printf "%s./%s -d /var/www/includes --filter 'foo*'%s\n\n" "$cyan" "$scriptName" "$norm"
printf "Generate SHA256 hashes for all '.css' files in /var/www/css and 'script.js' in /var/www/js/:\n"
printf "%s./%s -2 -f /var/www/js/script.js -d /var/www/css --filter '*.css'%s\n\n" "$cyan" "$scriptName" "$norm"
printf "N.B. If you copy this script to somewhere in your path, like /usr/local/bin, then you can run it like any other command without having to specify a leading path ('./' in above examples):\n"
printf "%s%s -f /var/www/css/styles.css%s\n\n" "$cyan" "$scriptName" "$norm"
exit 0;
}