danish

danish

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?

Showing Posts 1 to 7

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.

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.

danish

danish OP

Thanks for your prompt response @jordan0day , @Eiji , @easco
I needed to convert the map/json to string in order to match the hashed value.
I am now unable to match the Signature, in the elixir code I have include the expected Base64 string:

This is the signature code in C#:

public static string GenerateDigitalSignature(string body, string privateKeyText)
{
var hash = HashBody(body);

byte[] signedHash;
using (var privateKey = ImportPrivateKey(privateKeyText))
{
signedHash = privateKey.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
Console.WriteLine("signedHash");
}

var encodedHash = Convert.ToBase64String(signedHash);
Console.WriteLine(encodedHash);

return encodedHash;
}

and this is my code in elixir

def test(conn, _params) do
{:ok, rawSkey} = File.read("/home/danish/www/apis/signtest/keys/private_key.pem")

[encSKey] = :public_key.pem_decode(rawSkey)
sKey = :public_key.pem_entry_decode(encSKey)

string = "{\"MachineName\":\"DESKTOP-04VG548\",\"Userame\":\"inder\",\"Timestamp\":\"2018-10-26T20:58:48.7840832Z\"}"

msg = :crypto.hash(:sha256, string)

signature = :public_key.sign(msg, :sha256, sKey,  [{:rsa_padding, :rsa_pkcs1_padding}])
           |> Base.encode64

#
IO.inspect("signature")
IO.inspect(signature)
IO.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s7RN9jniIxJGJYLDSwVngOaFt8SVe95r4tBw4Kr28T66E7WP8xyLwih6ZA
/+QVQr2f4WZtKSyHxDgfkCh3lvzAEXPBCs782peIYGECgYEA9dlgqaKxyqZwFsvK
dMTTSK9MgqOLKKv16AUMh+Zmvt/kBvqAO6FHqqOw44jYV6XtleUnO54c87/wxtOd
KNHR0tp69fWChwPVAeN42Dby8nG5SMpauE7oi9TkULmBXXVuKFDHw1s9TZ3vw69c
tBiKglI+ulXhWvZNng6vso4YMZkCgYEA8n43dm4ux5kNSszqk3CmhJUMC6yw1/Yo
V1NRAsmJ+3HnRS61TFzij3KjYbO5e975nXxC+S+Mw3vb5PnOUTpIew/ABQyLBAHE
CkNXVr65O+r9ePgFmJEHK8cWF7o5tMO7CxfqJohJFn2u9dq/WuaVhTxZN0r5TIBo
8tYupvsg+u0CgYEA3B5zM68drKn0thRlAsOvwOZjhJxVXmcI12Kd45fg6omfqrfi
1IEEn73MBQ1jVqaT4J+5zuE0XvG97OdPUVr7mORRL6NP6P7/Wp+GdWBNidFU6d1z
+I1+HKTSf4x4TWt049ff5APvEW2VXQnH4OAQM7rbuw5lpYHd4OSZXy9mbhECgYAi
Yo1gyLzvZp+A5M+6enXSRQRBP4qQBQKxZ0oMeCGNuxgNMlNY8a7aomnSd9asXvDM
SQuCJCD0s0J6FIYR9I7EjHYxARdRSSLQRb5DuaWwfmDU326DtMTsiz1xveFi5YJH
T2vPbPHYM5g0moEZ1aqMCwOnmIUDOQ4oqQBSgzeo/QKBgQCJMGKVdG9zVr/za4fS
mQaRm+U8gHoohozgczsD9dB9dHgm7KEXhi+S6LhQfks0GnQvg9MuNfW5zrYXf+rq
DVCOF6jtp3hHDEd+KdDUgikF3FnwGzcq8e1hRLGvcIvqWEb+psimekk1aaAgUyCj
2JOy9eD5+cReQ1vL3Eh6Vtg5vw==-----END PRIVATE KEY-----

what do you reckon I am doing wrong?

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.

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
danish

danish OP

thank you @voltone, this resolves my issue.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews