# Elixir/Erlang equivalent of Ruby OpenSSL

**URL:** <https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560>\
**Category:** Questions\
**Tags:** crypto\
**Created:** [October 27, 2018, 11:12am UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560 "2018-10-27T11:12:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandergroen](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sandergroen/32/10132_2.png) [@sandergroen](https://forum.elixirforum.com/u/sandergroen)\
**Post date:** [October 27, 2018, 11:12am UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/1 "2018-10-27T11:12:15Z")

</div>

Hi everyone,

I like to know how to how to implement the following Ruby code in Elixir. I guess that I need to look in the Erlang crypto reference but I can’t figure out how to translate the code to Elixir/Erlang.

```
group = OpenSSL::PKey::EC::Group.new("prime256v1")
key = OpenSSL::PKey::EC.new(group)
pk_bn = OpenSSL::BN.new(pk_bytes, 2)
pk = OpenSSL::PKey::EC::Point.new(group, pk_bn)

```

I already found how to generate the key.

`:crypto.generate_key(:ecdh, :prime256v1)`

But how to create the public key bignum (pk\_bn) and the public key (pk)?

ps the pk\_bytes variable is passed in as a parameter.

---

<div class="post-metadata">

**Author:** ![jeremyjh](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeremyjh/32/5995_2.png) [@jeremyjh](https://forum.elixirforum.com/u/jeremyjh)\
**Post date:** [October 27, 2018, 12:41pm UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/2 "2018-10-27T12:41:17Z")

</div>

Sorry I don’t know exactly what that Ruby code is doing, but probably the app you are looking for is `:public_key`:

> **[Examples — OTP 29.0.2 (public\_key 1.21.2)](https://www.erlang.org/doc/apps/public_key/using_public_key.html)**

---

<div class="post-metadata">

**Author:** ![sandergroen](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sandergroen/32/10132_2.png) [@sandergroen](https://forum.elixirforum.com/u/sandergroen)\
**Post date:** [October 27, 2018, 3:11pm UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/3 "2018-10-27T15:11:08Z")

</div>

Hi Jeremujh,

If I look at the source from the Ruby [documentation](https://ruby-doc.org/stdlib-2.5.3/libdoc/openssl/rdoc/OpenSSL/BN.html). It looks like the OpenSSL function BN\_bn2bin [docs](https://www.openssl.org/docs/manmaster/man3/BN_bn2bin.html) is called from Ruby. I’m not sure what the equivalent in Erlang is but I will have a look at the link you suggested.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [October 27, 2018, 3:29pm UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/4 "2018-10-27T15:29:12Z")

</div>

Does the following link give you a start?

I know it’s not directly related but I had to make an Elixir app that can parse Rails authentication cookies a while ago and this helped me.

EDIT: oops, gave wrong link the first time.

[https://github.com/cconstantin/plug\_rails\_cookie\_session\_store](https://github.com/cconstantin/plug_rails_cookie_session_store)

---

<div class="post-metadata">

**Author:** ![cmkarlsson](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmkarlsson/32/23000_2.png) [@cmkarlsson](https://forum.elixirforum.com/u/cmkarlsson)\
**Post date:** [October 27, 2018, 10:22pm UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/5 "2018-10-27T22:22:14Z")

</div>

I don’t think you will find the same crypto primitives to do what you want directly from erlang/elixir. The ruby OpenSSL seems to be almost a one-to-one wrapper with OpenSSL whereas the erlang `crypto` NIF provides a higher abstraction.

The question is what you are trying to achieve? If the code was a function, what is the expected in and out parameters? From the look of the code above you have an incoming parameter `pk_bytes` (what is pk\_bytes?). What do you want to return?

---

<div class="post-metadata">

**Author:** ![sandergroen](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sandergroen/32/10132_2.png) [@sandergroen](https://forum.elixirforum.com/u/sandergroen)\
**Post date:** [October 28, 2018, 8:40am UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/6 "2018-10-28T08:40:05Z")

</div>

[dimitarvp](https://forum.elixirforum.com/u/dimitarvp), thanks I will have a look at the sources you provided.

Hi [cmkarlsson](https://forum.elixirforum.com/u/cmkarlsson),

Indeed the Ruby OpenSSL classes are a wrapper. But I guess that the same goal can be achieved with Erlang somehow.

I’m writing a [WebAuthn](https://www.w3.org/TR/webauthn/) package on [Github](https://github.com/sandergroen/web_authn_ex) inspired by [webauthn-ruby](https://github.com/cedarcode/webauthn-ruby). You can find the original sources of this method [here](https://github.com/cedarcode/webauthn-ruby/blob/master/lib/webauthn/authenticator_assertion_response.rb).

The `pk_bytes` variable (this must be public\_key\_bytes actually) is a public key in bytes. The function verifies data from an authenticator using this public key. I found this Erlang code in the documentation `crypto:verify(rsa, sha, <<"The message">>, Signature, PublicKey)` I guess this does what I need as an output for this function.

---

<div class="post-metadata">

**Author:** ![voltone](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voltone/32/24672_2.png) [@voltone](https://forum.elixirforum.com/u/voltone)\
**Post date:** [October 28, 2018, 9:14am UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/7 "2018-10-28T09:14:45Z")

</div>

So if I understand correctly, `pk_bytes` is a raw ECC public key without a curve identity, just a point. The curve is hardcoded to `prime256v1`.

In that case you should be able to verify a signature like this:

```elixir
public_key = {{:ECPoint, pk_bytes}, {:namedCurve, :prime256v1}}
:public_key.verify(message, :sha256, signature, public_key)

```

---

<div class="post-metadata">

**Author:** ![sandergroen](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sandergroen/32/10132_2.png) [@sandergroen](https://forum.elixirforum.com/u/sandergroen)\
**Post date:** [October 28, 2018, 10:34am UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/8 "2018-10-28T10:34:30Z")

</div>

Hi voltone,

That looks promising thanks! Unfortunately I don’t have time to try it right away but I will give that a try perhaps somewhere tomorrow.

---

<div class="post-metadata">

**Author:** ![sandergroen](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sandergroen/32/10132_2.png) [@sandergroen](https://forum.elixirforum.com/u/sandergroen)\
**Post date:** [November 21, 2018, 7:12pm UTC](https://forum.elixirforum.com/t/elixir-erlang-equivalent-of-ruby-openssl/17560/9 "2018-11-21T19:12:24Z")

</div>

It took a while before I finally was able to test this but it works like a charm. Thanks Voltone!
