ueno / ruby-gpgme

a ruby interface to GnuPG Made Easy (GPGME).
GNU Lesser General Public License v2.1
232 stars 99 forks source link

How to get ID / email after import? #150

Open rathboma opened 2 years ago

rathboma commented 2 years ago

Hey folks,

Firstly - THANK YOU for making this library. Amazing work and I don't know what I'd do without it.

I'm building a system in which users can upload their (private) gpg keys for signing files. So they give me a mycompany.key private key.

Using this, I can successfully run GPGME::Key.import(File.open('mycompany.key')), but...how do I identify the key after that?

This gives me a ImportResult, but I can't find any sort of ID / Email field on that object I can use....

Any ideas? Sorry if this is a dumb question, I'm not super experienced with the GPG utils

Carlgo11 commented 1 year ago

I realize this is a very old ticket but I came across the same issue and thought someone else might too in the future.

What you're going to want to do is get the imports objects from the ImportResult and then get the fingerprint from that.

Example:

imports = GPGME::Key.import(key).imports
fingerprints = imports.map(&:fpr)

You'll end up with an array of strings representing the fingerprints (SHA2) of all imported keys.

With that information, you can then fetch the actual keys as Key objects using:

keys = fingerprints.map { |fpr| GPGME::Key.find(fpr) }
kernelsmith commented 2 months ago

@rathboma take a look at https://github.com/ueno/ruby-gpgme/blob/master/lib/gpgme/misc.rb#L65 you can call imports as @Carlgo11 suggested, but depending on what you want to do exactly, you might be able to use

    considered, imported, unchanged
    new_user_ids, new_sub_keys, new_signatures
    not_imported,  imports

In GPG speak, when you import keys, they are checked before being imported, so if you import 5 keys, 5 keys will be considered and if they're already imported and nothing has changed, they're considered "unchanged". If some of the 5 keys are new and import w/o any errors, they are considered "imported". In your case, you may want new_signatures and/or new_user_ids, but I didn't check that those methods do exactly what you need, so YMMV