Skip to content

Instantly share code, notes, and snippets.

@ar45
Created December 30, 2019 20:28
Show Gist options
  • Save ar45/2264b17bfaf77542c55a5cb37463247e to your computer and use it in GitHub Desktop.
Save ar45/2264b17bfaf77542c55a5cb37463247e to your computer and use it in GitHub Desktop.
JWT RSA using shell script
#!/bin/sh
# JWT TOKEN signing using openssl private key RS256
base64WithoutPadding()
{
cat | openssl base64 -A | sed 's/=*$//g'
}
_sign()
{
header="$1" && shift
payload="$1" && shift
secret="$1" && shift
b64Header=$(echo -n "$header" | base64WithoutPadding)
b64Payload=$(echo -n "$payload" | base64WithoutPadding)
mysig="$(echo -n "$b64Header.$b64Payload" | openssl dgst -sha256 -sign $secret | base64WithoutPadding | tr '/' '_' | tr '+' '-' )"
echo "$b64Header.$b64Payload.$mysig"
}
case $1 in
sign)
shift
_sign "$@"
;;
verify)
shift
_verify "$@"
;;
*)
echo "Invalid option." >&2 && exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment