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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
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
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
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
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
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
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
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
I would like to know what is the best IDE for elixir development?
New
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
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
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...
New
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
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









