rnpgp / ruby-rnp

Ruby bindings for librnp
https://www.rnpgp.org
MIT License
7 stars 3 forks source link

Give example of key generation with saving keys #12

Open ronaldtse opened 6 years ago

ronaldtse commented 6 years ago

I'd like to see something like this, couldn't figure out how to save generated keys in GPG or KBX formats...

# generate both a primary key and a subkey
generated = rnp.generate_key(
  primary: {
    type: 'RSA,
    length: 2048,
    userid: 'testuser',
    usage: [:sign]
  },
  sub: {
    type: 'RSA,
    length: 2048,
    usage: [:encrypt]
  }
)

# save??
dewyatt commented 6 years ago

https://github.com/riboseinc/ruby-rnp/issues/2#issuecomment-385835133

Did you mean you want an example added to the repo?

ronaldtse commented 6 years ago

Both 😉 Thank you @dewyatt !

ronaldtse commented 6 years ago

Is it possible to use keys generated via Rnp.new for start_encrypt?

Something like this?

rnp.load_keys(format: 'raw', input: Rnp::Input.from_XXX)
enc = rnp.start_encrypt(
  input: Rnp::Input.from_string(plaintext),
  output: Rnp::Output.to_null
)
ronaldtse commented 6 years ago

Oh, I see that by default Rnp.new uses 'GPG' for both:

https://github.com/riboseinc/ruby-rnp/blob/9293168068d9d9e019aefd5065bf290cbae23c63/lib/rnp/rnp.rb#L27

ronaldtse commented 6 years ago

In particular, is it possible to only rnp.save_keys on particular keys?

dewyatt commented 6 years ago

I would like to get rid of pubfmt and secfmt for Rnp#initialize eventually as it does seem out of place (related TODO).

In particular, is it possible to only rnp.save_keys on particular keys?

Yes, but not with the #save_keys interface. I recall you can do File.write with the #public_key_data or #secret_key_data.

ronaldtse commented 6 years ago

Indeed #public_key_data and #secret_key_data works, but once exported, how can we read them back as keys? Rnp.key_format(data) return nil on these.

dewyatt commented 6 years ago

Rnp.key_format(data) return nil on these.

I wouldn't expect it to so that would probably be a bug.

ronaldtse commented 6 years ago

I wouldn't expect it to so that would probably be a bug.

Actually it works. Not sure what I did last night, probably just too late 👍

dewyatt commented 6 years ago

It could be that you followed my bad advice and used File.write instead of File.binwrite. Glad it's working either way.

ronaldtse commented 6 years ago

You're probably right. I didn't try saving today 😉

ronaldtse commented 6 years ago

I think the example should show key generation, key save (in GPG/KBX/ASCII) formats and a key load.

@dewyatt does it make more sense that we utilize a Key model on the Ruby side to manage such functionality?