danish

danish

Convert C# function to Elixir

this is the function that I am trying to convert to Elixir:

private static byte[] HashBody(string content)
   {
       var contentBytes = Encoding.UTF8.GetBytes(content);

       using (var provider = new SHA256Managed())
       {
          var hash = provider.ComputeHash(contentBytes);
          return hash;
        }
    }

My attempt:

content = %{
  "id" => "123",
  "name" => "Jaqob"
} |> Poison.encode!

digest = :crypto.hash(:sha256, content)

I want to convert content variable to bytes which is where I am stuck, also is the hashing function correct?

Marked As Solved

voltone

voltone

BTW, just FYI and FWIW, a bit more idiomatic Elixir would be:

def test(conn, _params) do
  raw_skey = File.read!("/home/danish/www/apis/signtest/keys/private_key.pem")

  [enc_skey] = :public_key.pem_decode(raw_skey)
  skey = :public_key.pem_entry_decode(enc_skey)

  string = ~s("MachineName":"DESKTOP-04VG548","Userame":"inder","Timestamp":"2018-10-26T20:58:48.7840832Z"})

  signature =
    string
    |> :public_key.sign(:sha256, skey,  rsa_padding: :rsa_pkcs1_padding)
    |> Base.encode64

  IO.puts("signature")
  IO.inspect(signature)
  json conn, "ok"
end

Also Liked

jordan0day

jordan0day

I think @easco’s answer is pretty much correct, but I’m guessing what you’re after is maybe a list of bytes? Since your C# code returns an array of bytes? You can use :erlang.binary_to_list/1 on digest to convert the binary to a list.

voltone

voltone

This line (above) is unnecessary: your call to :public_key.sign already calculates a SHA256 over the input, so right now the input string is hashed twice. Just pass string instead of msg to the sign function.

easco

easco

Your content variable is already a Binary (an Elixir string is a Binary) so it is already “bytes”.

Your code looks complete to me. Where are you having problems?

Last Post!

danish

danish

thank you @voltone, this resolves my issue.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement