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?

Eiji

Eiji

How about using :erlang.term_to_binary/1? With this you can work with any data you want unlike Poison.

danish

danish

thank you @voltone, this resolves my issue.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
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 record...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement