Created
December 30, 2019 20:28
-
-
Save ar45/2264b17bfaf77542c55a5cb37463247e to your computer and use it in GitHub Desktop.
JWT RSA using shell script
This file contains hidden or 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/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