You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.5 KiB
43 lines
1.5 KiB
#!/bin/bash
|
|
|
|
# Generate a JWT, with signature verified using OpenSSL
|
|
#
|
|
# Prerequisites: openssl, jq
|
|
#
|
|
# Usage: source ./openssl_signing_console.sh
|
|
#
|
|
# For a more complete explanation, see ./openssl_signing_console.rst
|
|
#
|
|
# It's crazy that raw execution only works about 20% of the time!
|
|
# See https://stackoverflow.com/questions/77505582/why-would-openssl-verify-succeed-every-time-with-source-but-fail-80-of-the
|
|
|
|
|
|
openssl ecparam -name secp256k1 -genkey -noout -out private.pem
|
|
openssl ec -in private.pem -pubout -out public.pem
|
|
|
|
header='{"alg":"ES256K", "issuer": "", "typ":"JWT"}'
|
|
|
|
payload='{"@context": "http://schema.org", "@type": "PlanAction", "identifier": "did:ethr:0xb86913f83A867b5Ef04902419614A6FF67466c12", "name": "Test", "description": "Me"}'
|
|
|
|
header_b64=$(echo -n "$header" | jq -c -M . | tr -d '\n' | base64 | tr -d '=' | tr '+' '-' | tr '/' '_')
|
|
payload_b64=$(echo -n "$payload" | jq -c -M . | tr -d '\n' | base64 | tr -d '=' | tr '+' '-' | tr '/' '_')
|
|
|
|
signing_input="$header_b64.$payload_b64"
|
|
|
|
signature=$(echo -n "$signing_input" | openssl dgst -sha256 -sign private.pem)
|
|
|
|
echo -n "$signing_input" | openssl dgst -sha256 -verify public.pem -signature <(echo -n "$signature")
|
|
|
|
# Also tested this, to no avail.
|
|
#echo -n "$signature" > sig.out
|
|
#echo -n "$signing_input" | openssl dgst -sha256 -verify public.pem -signature sig.out
|
|
|
|
|
|
|
|
# Read binary signature and encode it to Base64 URL-Safe format
|
|
signature_b64=$(echo -n "$signature" | base64 | tr -d '=' | tr '+' '-' | tr '/' '_')
|
|
|
|
# Construct the JWT
|
|
jwt="$signing_input.$signature_b64"
|
|
|
|
echo Resulting JWT: $jwt
|
|
|