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
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
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
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
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
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
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
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
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’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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









