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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement