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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews