Skip to content

Instantly share code, notes, and snippets.

@redshiftzero
Created April 14, 2020 15:09
Show Gist options
  • Save redshiftzero/cd206ca16495d39a0853f83c6fcad289 to your computer and use it in GitHub Desktop.
Save redshiftzero/cd206ca16495d39a0853f83c6fcad289 to your computer and use it in GitHub Desktop.
converting armored gpg public key to JWK format - without private key
from authlib.common.encoding import int_to_base64
import json
import pgpy
# Input is PGP armored pubkey
key, _ = pgpy.PGPKey.from_file('key.asc')
n = int(key._key.keymaterial.n)
e = int(key._key.keymaterial.e)
# Expected JWK format according to https://tools.ietf.org/html/rfc7518#section-6.1
pubkey = {
'kty': 'RSA',
'e': int_to_base64(e),
'n': int_to_base64(n)
}
print(json.dumps(pubkey))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment