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
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
5
Also Liked
jordan0day
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.
3
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?
1
Eiji
How about using :erlang.term_to_binary/1? With this you can work with any data you want unlike Poison.
1
Popular in Questions
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
can someone please explain to me how Enum.reduce works with maps
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
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
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
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
Other popular topics
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
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
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New







