Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

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 did find the hexa value of the key in the binary file, however 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.

thank you.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer

You can use gpg --dry-run to prevent changes.

Following line will print the key id in its output (can be modified using the usual modifiers like --with-colons for further processing).

gpg --dry-run --import pubkey.asc

Just tried it, the key did not get stored to my keychain, but the key id was printed. But watch out with --dry-run, the man page has a warning:

   --dry-run
         Don't make any changes (this is not completely implemented).
share|improve this answer

To avoid the "not completely implemented" issue mentioned in Jens Erat's answer, use gpg --homedir on a temporary directory. You may need to modify the mktemp command based on your platform:

gpg --homedir $( mktemp -d -t '' ) --import /tmp/somekey.asc
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.