sandergroen

sandergroen

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.

Showing Posts 1 to 8

jeremyjh

jeremyjh

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

sandergroen

sandergroen OP

Hi Jeremujh,

If I look at the source from the Ruby documentation. It looks like the OpenSSL function BN_bn2bin docs 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.

dimitarvp

dimitarvp

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

cmkarlsson

cmkarlsson

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?

sandergroen

sandergroen OP

dimitarvp, thanks I will have a look at the sources you provided.

Hi 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 package on Github inspired by webauthn-ruby. You can find the original sources of this method here.

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.

voltone

voltone

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:

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

sandergroen OP

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.

sandergroen

sandergroen OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

JosePamplona
Hi everyone, Over several Elixir projects I kept writing down what each one taught me and turning the setups I got right into something ...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews