Created
October 16, 2017 23:38
-
-
Save cypnk/91fdbdb6ea402435011838d8883a72b4 to your computer and use it in GitHub Desktop.
Sign a file with a given private key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sign a file using the specified private key | |
# Source file to sign | |
SRCF=$1 | |
# Private key location | |
PRIK=$2 | |
# Signature algorithm (defaults to SHA-512) | |
ALGO=${3:-sha512} | |
# Algo file | |
ALGF=$SRCF.$ALGO.sig | |
# Prevent overwrite | |
if [ -f "$ALGF" ]; then | |
echo "Signature file already exists" | |
exit | |
fi | |
if [ -f "$PRIK" ]; then | |
if [ -f "$SRCF" ]; then | |
openssl dgst -$ALGO -sign $PRIK -out $ALGF $SRCF | |
else | |
echo "Source file not found" | |
fi | |
else | |
echo "Private key not found" | |
fi | |
exit | |
# To use this, run: | |
# sh sign.sh /path/to/file.ext /path/to/privatekey.pem | |
# A signature file will be created in "file.ext.sha512.sig" format | |
# To use a different algorithm E.G. SHA-256: | |
# sh sign.sh /path/to/file.ext /path/to/privatekey.pem sha356 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment