Extracting the PGP keyid from the public key file

  • Is there a way via an CLI tool or some kind of API to extract the PGP key ID from the PGP public key block?

    I found the hexa value of the key in the binary file, but I guess the position is based on the key kind/size.

    Basically, I have the base64 formatted public key and I would like to retrieve the key ID from it, without importing it with GnuPG.

  • Jor-el

    Jor-el Correct answer

    8 years ago

    RFC 4880 on OpenPGP message format talks about how to calculate key ID from public key.

    Excerpts from section 12.2:

    For a V3 key, the eight-octet Key ID consists of the low 64 bits of the public modulus of the RSA key.

    And for V4 keys:

    A V4 fingerprint is the 160-bit SHA-1 hash of the octet 0x99, followed by the two-octet packet length, followed by the entire Public-Key packet starting with the version field. The Key ID is the low-order 64 bits of the fingerprint.

    You can easily parse the last 64 bits from the base64 encoded public keys, which is the key ID for the corresponding public key.

    could you please elaborate more on "easily parse the last 64 bits..."? I generated a pgp key (Got its MPI in base64 format. Is it what is meant by public key?), copy-n-pasted it to here, for example, and counted last 8 bytes in hex representation. Is that correct?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM