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
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews