Skip to content

Instantly share code, notes, and snippets.

@sathishshan
Created April 13, 2024 06:26
Show Gist options
  • Save sathishshan/9b61c691e3f7013d4b0ab6fff80a70d6 to your computer and use it in GitHub Desktop.
Save sathishshan/9b61c691e3f7013d4b0ab6fff80a70d6 to your computer and use it in GitHub Desktop.
HS256 - Signature:
echo -n "<JWT Base64url encoded - Header.Payload>" | openssl dgst -sha256 -hmac '<SECRET>' -binary | openssl base64
The output of the signature will give base64 encoding, convert that to base64url encoding
SZf1eovdqV+1mo8rvI79UxQT3Ue/mJd3ipXu8XO01os=
Change (+ to -), (/ to _), (Omit the padding == or =)
SZf1eovdqV-1mo8rvI79UxQT3Ue_mJd3ipXu8XO01os
Automating the base64url encoding:
echo -n "<JWT Header.Payload" | openssl dgst -sha256 -hmac '<SECRET>' -binary | openssl base64 -e -A | sed 's/\+/-/g' | sed 's/\//_/g' | sed -E 's/=+$//' | xargs
RS256 - Signature:
Priv and Pub Key Generations
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -out public.pem -pubout -outform PEM
Signing with the Priv key
echo -n "<JWT Base64url encoded - Header.Payload>" | openssl dgst -sha256 -sign private.pem | openssl base64 -e -A | sed 's/\+/-/g' | sed 's/\//_/g' | sed -E 's/=+$//' | xargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment