-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ex_multihash instead of :crypto.hash(:sha512, input) #8
Comments
If you need more context on the IPFS/IPLD CID function, |
@nelsonic I forked the repo and installed excoveralls to check the code coverage. I know that they currently only have doctests but I figured if those doctests actually worked then all should be okay with them (unless I am misunderstanding why you might want separate tests)
There are no tests in the util file. There are only 2 function definitions however and one is a private function that is called by the other function. (What I am trying to say (UNLCEARLY) is that we would only need to test one function here) There is only one line in do: encode(function, digest, length) It is in the following function @spec encode(binary, binary, integer_default) :: on_encode
def encode(<<_hash_code>> = hash_code, digest, length) when is_binary(digest) do
with {:ok, function} <- get_hash_function(hash_code),
do: encode(function, digest, length)
end I have called some of the functions in an I can call it but I am not sure what argument to pass in to get past the following line with {:ok, function} <- get_hash_function(hash_code) do
as the With regards to this point
I wouldn't say that I fully understand everything about hashing (right now) but the elixir code in this module is very well written and fairly straightforward to understand. (Plus the examples are pretty great) |
@RobStallion Did you push a commit to GitHub with your addition of excoveralls? |
@nelsonic the doc tests do appear to be testing the functions iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"))
{:ok, <<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>} This calls the functions with the above arguments and expects the result below. These results can be replicated in your iex shell as well. Also, if you change the returned binary in anyway the test fails. The warnings all appear to be of the following... The only reason this appears to be a thing is because the devs decided to put documentation on their private helper functions. |
Yeah, Docs in private functions are a good thing. I don't know why Elixir considers them a warning. 🙄 |
@nelsonic Upon further inspection, the This is probably the reason that they did not include the tests in the first place. (I should have noticed this on my first pass through 😞) I can and have added tests for this now (in the same doc test format as the maintainers) and opened a pr here. Do you still want me to open a PR with them? |
The code below is a modified version of the current It can be tidied and the variables can be given better names (didn't think this was important for proof of concept) but it shows that we can use Original def make(input, length \\ 32) do
hash = :crypto.hash(:sha512, input)
hash
|> Base.encode64()
|> String.replace(~r/[Il0oO=\/\+]/, "", global: true)
|> String.slice(0..(length - 1))
end With def make(input, length \\ 32) do
hash1 = :crypto.hash(:sha512, input)
{:ok, <<_multihash_code, _length, hash2::binary>>} = Multihash.encode(:sha2_512, hash1)
hash2
|> Base.encode64()
|> String.replace(~r/[Il0oO=\/\+]/, "", global: true)
|> String.slice(0..(length - 1))
end Multihash takes a hash and prepends info onto it.
In the example above I am simply pattern matching the first 2 values, doing nothing with them and using the hash2 value in what was the original function call. This is not how we would use it in production I'm sure, but it does show what we can use the |
@RobStallion this is a good start. thanks! 👍 |
@RobStallion using |
GOTO: #11 |
At present, the Erlang
:crypto.hash(:sha512, input)
is called directly when creating acid
:cid/lib/cid.ex
Line 25 in 3e8eb82
In light of the fact that https://github.com/ipld/cid achieves a pretty similar goal,
I propose that we use
multihash
instead of directly creating a:sha512
hash.This will ensure forward compatibility and also mean that we can use the JS
cid
function on the client.Todo
https://github.com/multiformats/ex_multihash/blob/dd3ce7e39d053049dbd7219c2aba2dbde9940bb2/test/multihash_test.exs#L3
The text was updated successfully, but these errors were encountered: