EinerZG
The hash generated in Elixir does not match the Javascript hash
Hi all, I have a problem with the generation of a hash. I have a Javascript code that takes the body of a request, passes it to a string and then generates a hash. I need to pass the code to Elixir, but the results do not match. I share the code with you:
JS
import sha256 from 'crypto-js/sha256';
import Base64 from 'crypto-js/enc-base64';
const body = "my body"
const hashDigest = sha256(body);
const digest = Base64.stringify(hashDigest)
console.log("Digest:", digest);
Result: "IZ2yn/XIIEdwXSkGAK/256XFSNMy33gr0zRZoo+pe/c="
Elixir
def compute_digest(body) do
body
|> Jason.encode!()
|> (&:crypto.hash(:sha256, &1)).()
|> Base.encode64()
|> IO.inspect(label: "Digest")
end
Result: "7om2lOXEa2byLhn1M19iEf9KA6DdnljHD6BMdA8IdLc="
Any ideas on how to solve this problem?
Marked As Solved
benwilson512
A very important thing to keep in mind is JSON key order. Neither the JSON spec nor the Elixir maps you generally encode to JSON have any guaranteed key order, so when you write it out to a string you may get different actual strings before the encoding. That may not matter for you use case but it’s a common issue if you expect strict equality with larger or more complex JSON strings.
Also Liked
derek-zhou
If you remove the Jason.encode!() then it will match:
iex(2)> :crypto.hash(:sha256, "my body") |> Base.encode64()
"IZ2yn/XIIEdwXSkGAK/256XFSNMy33gr0zRZoo+pe/c="
EinerZG
Yes, thank you, yesterday I was able to identify that this is the problem. When getting the body it is changing the odern of the keys. Actually the string transformation that the Jason.encode!/1 does does not affect the hash generation. Thanks for the help.
cevado
js snippet and elixir snippet doesn’t do the same thing, you need to pass body to JSON.stringify for the js snippet to be the same, something like:
import sha256 from 'crypto-js/sha256';
import Base64 from 'crypto-js/enc-base64';
const body = JSON.stringify("my body")
const hashDigest = sha256(body);
const digest = Base64.stringify(hashDigest)
console.log("Digest:", digest);
edit: removed the result because i didn’t run the code here.
dimitarvp
Show us the exact value of body in both codebases and make VERY sure they are both "hello" and don’t have double-double quotes in Elixir.







