Get apt’s key ids and fingerprints in machine-readable format

aptgpg

I'm trying to patch an issue in puppetlabs-apt to enable the use of key fingerprints as identifiers to ensure that a certain key is present by its 40-digit key fingerprint.

I'm having difficulty checking that the key is present and I need a command which will output the following:

  • The 8-digit ID of the key.
  • The 16-digit ID of the key.
  • The 40-digit ID of the key.

Is there an apt-key command I can use to output these values, one per line, so I can parse the output and check if the key is present?

Best Answer

apt-key adv will let you directly pass options to GnuPG.

So you can do something like this to get parseable outpout:

# apt-key adv --list-public-keys --with-fingerprint --with-colons 
⋮
fpr:::::::::126C0D24BD8A2942CC7DF8AC7638D0442B90D010:
pub:-:4096:1:9D6D8F6BC857C906:2014-11-21:2022-11-19::-:Debian Security Archive Automatic Signing Key (8/jessie) <ftpmaster@debian.org>::scSC:
fpr:::::::::D21169141CECD440F2EB8DDA9D6D8F6BC857C906:
⋮

Since you're only interested in the fingerprint (the 8-digit and 16-digit IDs are just the end of the fingerprint), | grep ^fpr would seem to give you the lines you care about.

Related Question