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
I would like to know what is the best IDE for elixir development?
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
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
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
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
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
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Other popular topics
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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









