# Convert C# function to Elixir

**URL:** <https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517>\
**Category:** Questions\
**Tags:** erlang, security, crypto, encryption, hash\
**Created:** [October 25, 2018, 2:08pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517 "2018-10-25T14:08:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![danish](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@danish](https://forum.elixirforum.com/u/danish)\
**Post date:** [October 25, 2018, 2:08pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/1 "2018-10-25T14:08:19Z")

</div>

this is the function that I am trying to convert to Elixir:

```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:

```Elixir
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?

---

<div class="post-metadata">

**Author:** ![easco](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/easco/32/1250_2.png) [@easco](https://forum.elixirforum.com/u/easco)\
**Post date:** [October 25, 2018, 3:04pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/2 "2018-10-25T15:04:30Z")

</div>

> [@danish](#):
>
> I want to convert content variable to bytes which is where I am stuck

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?

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 25, 2018, 3:05pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/3 "2018-10-25T15:05:28Z")

</div>

> [@danish](#):
>
> I want to convert content variable to bytes which is where I am stuck, also is the hashing function correct?

How about using `:erlang.term_to_binary/1`? With this you can work with any data you want unlike `Poison`.

---

<div class="post-metadata">

**Author:** ![jordan0day](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jordan0day/32/896_2.png) [@jordan0day](https://forum.elixirforum.com/u/jordan0day)\
**Post date:** [October 25, 2018, 3:12pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/4 "2018-10-25T15:12:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danish](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@danish](https://forum.elixirforum.com/u/danish)\
**Post date:** [October 29, 2018, 5:46pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/5 "2018-10-29T17:46:38Z")

</div>

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.inspect("EXPECTED SIGNATURE")
IO.inspect("1FdzHD5FhYFEbV9NpbXpE/CfrvHcv8p/LDQxMd900iZQxrSNbNqwQ/3coWO2Pof3nCmRIPicURyvp3+UrRALp/4eEFprdzmITFh9ojVrOvFI0FYqDsvEDQYvw6G2oX6vVw0b640dg3a+J3A8KEUs3RwSuiilSiqi/CeUzRNFhI3+7aPYF5p0QrAnoNmgmK6GbIY6eu0WnMxyJcGsrkE2KYWCqxrfMq5AiKIPdZVdXfLM6+0AoVw2Q7EgUILNRA91v2QGkdpBwvpHX5Feau2NspS8sq8zdTz8wEVHXC0GjLMqK5M1VvWAW+evYi15qqPOtYHXrvN2KJXjEwSbFdRc7A==")
json conn, "ok"

```

end

private\_key:

```
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDo4LOhXxF5f3yU
BhOsyv4wiIn2aMpCiFYOd4X/TufdgBZc4DcTkXOqQ0piIcgnWN0pp9ZTXc5Fmj51
UO2/bs7uZedi/Q7zcHpuI8DB9tCa82cuGkx1uHzTRzQF3cEbkZD/7pC6mopCGOgJ
y8rGZWuKPzcyhNgmo9J0ou04Nkqvzsd3O8STaGd1eF3Q+eb2rWgSLOfYsuO4Fu13
JZJK+IJYA5oj5ci132KEi6LPK9HDHbb4u1ie5J1GvMgVUSD+MK4IP4lcJBbpQP5f
1dAJ4Y2/OTqv66YNeSSOdrVq36l2iSKErImk7B9GjKCuDAMLBgllI/nGgSRK5EdQ
Ke5i9VSlAgMBAAECggEBAImY8yo/JPEYDcDcklV/n4+HrZTFt/R13/suTiNbOQ8c
dpor9P7/60rH9bRj54KwNAU/4RhjDdT8XTkuNzvsOiKMXZqUkXfvlrdrMqKotftI
JEgOS+0ms8KWiLuKmmYyrW07RZKsa1LAqqHXIxn0pQULcw7LhXfXRiuadPXI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PRIVATE KEY-----

```

what do you reckon I am doing wrong?

---

<div class="post-metadata">

**Author:** ![voltone](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voltone/32/24672_2.png) [@voltone](https://forum.elixirforum.com/u/voltone)\
**Post date:** [October 29, 2018, 6:27pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/6 "2018-10-29T18:27:27Z")

</div>

> [@danish](#):
>
> msg = :crypto.hash(:sha256, string)

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.

---

<div class="post-metadata">

**Author:** ![voltone](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voltone/32/24672_2.png) [@voltone](https://forum.elixirforum.com/u/voltone)\
**Post date:** [October 29, 2018, 6:35pm UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/7 "2018-10-29T18:35:15Z")

</div>

BTW, just FYI and FWIW, a bit more idiomatic Elixir would be:

```elixir
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

```

---

<div class="post-metadata">

**Author:** ![danish](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@danish](https://forum.elixirforum.com/u/danish)\
**Post date:** [October 30, 2018, 9:24am UTC](https://forum.elixirforum.com/t/convert-c-function-to-elixir/17517/8 "2018-10-30T09:24:19Z")

</div>

thank you @voltone, this resolves my issue.
