aligredo

aligredo

Help with :crypto.hash

Hello there,
I’m trying to generate a checksum using :crypto.hash/2, I want to hash the first 8 characters only of my string and get the result as a decimal value. More precisely I’m trying to generate the checksum using SHA1, in Javascript it would be something like that parseInt(sha1(str).substr(0, 8), 16); for a string “Hi?Hi.” it should be 174888039. Note the character “\x1f” between ? and H.

This is my code:

Integer.parse(:crypto.hash(:sha, String.slice("First Part" <> "\x1f" <> "Second Part", 0..8)), 16)

but it returns :error , any help with that?

Thanks in advance!

Marked As Solved

voltone

voltone

You can avoid the conversion to Hex:

iex(1)> :crypto.hash(:sha, "Hi?\x1fHi.") |> binary_part(0, 4) |> :crypto.bytes_to_integer()
174888039

Or:

iex(1)> <<int :: integer-size(32), _ :: binary>> = :crypto.hash(:sha, "Hi?\x1fHi.")
iex(2)> int
174888039

Also Liked

cmkarlsson

cmkarlsson

The digest is return as a binary representation.

iex(18)> :crypto.hash(:sha, "abc123")
<<99, 103, 196, 141, 209, 147, 213, 110, 167, 176, 186, 173, 37, 177, 148, 85,
  229, 41, 245, 238>>

So this binary representation must be converted to the integer you want. You are trying to use Integer.parse(..,16) which requires a hex representation.

So one option could be to convert the binary to hex first and then parse to integer. Or perhaps it is possible to pattern-match out the integer directly from the binary hash as hauleth is hinting at.

For example:

<<y::integer-size(160)>> = yourhash

I don’t know if that gives the same result as:

yourhash |> Base.encode(16) |> Integer.parse(16)

but if it does it saves a few functions and are generally cleaner.

hauleth

hauleth

Ok, what exactly you are trying to achieve. Give us an example input and example output of what you want, maybe then it will be easier to understand what you want.

hauleth

hauleth

Your main problem is thinking that 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 is SHA-1 output, it isn’t. Just check out what value does the crypto:hash/2 produce and work from there (hint, you can use pattern matching to generate integer from binary).

Where Next?

Popular in Questions Top

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement