ntrepid8 / ex_crypto

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

Implement Inspect for RSAPrivateKey #23

Closed obrok closed 6 years ago

obrok commented 6 years ago

This should make it less likely that it will accidentally get dumped in a log or on a screen.

ntrepid8 commented 6 years ago

I like this idea.

What about adding an option to really inspect the struct? That way if inspect was being used for debugging or something like that you could still see the internal state.

obrok commented 6 years ago

What about adding an option to really inspect the struct? That way if inspect was being used for debugging or something like that you could still see the internal state.

I'll try it out tomorrow and update the PR

obrok commented 6 years ago

Unfortunately Inspect only allows a specific set of options through, so there doesn't seem to be a way to add a custom one. What you can do, even with this patch, is use structs: false, which will dump the structure as a map:

iex(2)> IO.inspect(%ExPublicKey.RSAPrivateKey{}, structs: false)
%{
  __struct__: ExPublicKey.RSAPrivateKey,
  ctr_coefficient: nil,
  exponent_one: nil,
  exponent_two: nil,
  other_prime_infos: nil,
  prime_one: nil,
  prime_two: nil,
  private_exponent: nil,
  public_exponent: nil,
  public_modulus: nil,
  version: nil
}
ntrepid8 commented 6 years ago

Ok, let's go with what you already had. Thanks for the PR! I'll work on getting this deployed to hex.pm ASAP.

obrok commented 6 years ago

Thanks!

ntrepid8 commented 6 years ago

@obrok if you have a moment checkout PR #24 because I made a few updates to the new inspect behavior. I still wanted to make it possible to identify a specific key in logs if inspect is used, so I updated it to include a SHA256 fingerprint, something I had been working on anyway.

Now it would look like this:

#ExPublicKey.RSAPrivateKey<fingerprint_sha256=7a:40:1c:b9:4b:b8:a5:bb:6b:98:b6:1b:8b:7a:24:8d:45:9b:e5:54:17:7e:66:26:7e:95:11:9d:39:14:7b:b2>

I also added pretty-print support so if those options are used it will break correctly:

#ExPublicKey.RSAPrivateKey<
     fingerprint_sha256=7a:40:1c:b9:4b:b8:a5:bb:6b:98:b6:1b:8b:7a:24:8d:45:9b:e5:54
      17:7e:66:26:7e:95:11:9d:39:14:7b:b2>

Thoughts?

ntrepid8 commented 6 years ago

Published on hex.pm as v0.9.0: https://hex.pm/packages/ex_crypto/0.9.0