ntrepid8 / ex_crypto

Wrapper around the Erlang crypto module for Elixir.
MIT License
144 stars 48 forks source link

ExPublicKey.generate_key/2 fails. #26

Open hickscorp opened 6 years ago

hickscorp commented 6 years ago

Straight from the docs:

{:ok, key} = ExPublicKey.generate_key(:rsa, 2048)

But this is what I get when running it:

** (ArgumentError) argument error
    (crypto) :crypto.rsa_generate_key_nif(:rsa, <<8, 0>>)
    (crypto) crypto.erl:543: :crypto.generate_key/3
    (public_key) public_key.erl:426: :public_key.generate_key/1
    (ex_crypto) lib/ex_public_key.ex:287: ExPublicKey.generate_key/4

Using 0.9.0.

ntrepid8 commented 6 years ago

Which version of Elixir and Erlang are you using? What operating system?

hickscorp commented 6 years ago
 iex --version
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

IEx 1.6.4 (compiled with OTP 20)
uname -a
Linux Hostname 4.13.0-38-generic #43-Ubuntu SMP Wed Mar 14 15:20:44 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
ntrepid8 commented 6 years ago

I think I see what the issue is. I can probably get it fixed in the next couple days unless you would rather send a pull-request my way with an update.

hickscorp commented 6 years ago

@ntrepid8 Gladly yes. Tell me what's wrong and I'll fix it if I can.

tcitworld commented 6 years ago

@hickscorp The function you're calling is

def generate_key(bits, public_exp), do: generate_key(:rsa, bits, public_exp)

therefore you should call it like this

{:ok, key} = ExPublicKey.generate_key(2048, 65537)

or more simply

{:ok, key} = ExPublicKey.generate_key()

which has those default parameters.

hickscorp commented 6 years ago

@tcitworld Thanks, but what I was pointing out is a documentation inconsistency. See here: https://hexdocs.pm/ex_crypto/0.9.0/ExPublicKey.html#generate_key/0

The docs seem to define a passing doctest reading {:ok, rsa_priv_key} = ExPublicKey.generate_key(:rsa, 2048). However when you run it verbatim, it fails.