Skip to content

Instantly share code, notes, and snippets.

@cypnk
Created October 16, 2017 23:38
Show Gist options
  • Save cypnk/91fdbdb6ea402435011838d8883a72b4 to your computer and use it in GitHub Desktop.
Save cypnk/91fdbdb6ea402435011838d8883a72b4 to your computer and use it in GitHub Desktop.
Sign a file with a given private key
#!/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