Giter Club home page Giter Club logo

ex_multihash's Introduction

is project is no longer maintained and has been archived.

ex_multihash

hex.pm version API Docs Travis CI Inline docs

Multihash implementation in Elixir

This is the Multihash implementation in Elixir.

Table of Contents

Install

To use ex_multihash add to your mix.exs file:

defp deps do
  [
    {:ex_multihash, "~> 1.0"}
  ]
end

Usage

Encoding

Encode the provided hashed digest to the provided multihash of hash_code.

Examples

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>>}

iex> Multihash.encode(:sha2_256, :crypto.hash(:sha256, "Hello"))
{:ok, <<18, 32, 24, 95, 141, 179, 34, 113, 254, 37, 245, 97, 166, 252, 147, 139, 46, 38, 67, 6, 236, 48, 78, 218, 81, 128, 7, 209, 118, 72, 38, 56, 25, 105>>}

Invalid hash_code or digest corresponding to the hash function will return an error.

Examples

iex> Multihash.encode(:sha2_unknow, :crypto.hash(:sha, "Hello"))
{:error, "Invalid hash function"}

iex> Multihash.encode(0x20, :crypto.hash(:sha, "Hello"))
{:error, "Invalid hash code"}

It's possible to truncate a digest by passing an optional length parameter. Passing a length longer than the default digest length of the hash function will return an error.

Examples

iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 10)
{:ok, <<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>}
iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 30)
{:error, "Invalid digest length"}

Decoding

Decode the provided multihash to:

%Multihash{name: atom, code: integer, length: integer, digest: bitstring}

Examples

iex> Multihash.decode(<<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>)
{:ok, %Multihash{name: :sha1, code: 17, length: 20, digest: <<247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>}}

Invalid multihash will result in errors

Examples

iex> Multihash.decode(<<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171>>)
{:error, "Invalid size"}

iex> Multihash.decode(<<17, 22, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 20, 21, 22>>)
{:error, "Invalid digest length"}

iex> Multihash.decode(<<25, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>)
{:error, "Invalid hash code"}

iex> Multihash.decode(<<17, 32, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>)
{:error, "Invalid length of provided hash function"}

iex> Multihash.decode("Hello")
{:error, "Invalid hash code"}

Maintainers

Captain: @zabirauf.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the Readme, please conform to the standard-readme specification.

License

MIT ยฉ 2015 Zohaib Rauf.

ex_multihash's People

Contributors

candeira avatar richardlitt avatar robstallion avatar ruhlio avatar rvagg avatar sunny-g avatar zabirauf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ex_multihash's Issues

Move to Multiformats?

Hey @zabirauf!

Thanks so much for this. As you may know, we recently created the Multiformats organization to be a home for all of the multiformats - multiaddr, multihash, etc. Would you be interested in moving this repository to that organization? You would still have admin rights on the repository, but it would be a part of a wider organization. This would mean more relevant eyes on it (most likely) and better cross-repository issue tracking. We'd also add a line mentioning you as the original author, and of course your commits would stay the same.

Of course, keeping it on your profile is also cool; we'll still link to this from the main multihash repository at github.com/multiformats/multihash.

Thanks for taking the time to read this. ๐Ÿ‘

Tracking issue, here: multiformats/multiformats#4.

Monad dependency fail

I let you know that this library fails to compile because its Monad depency fails:

== Compilation error in file lib/monad.ex == ** (SyntaxError) lib/monad.ex:247: reserved token: __block__ (elixir) lib/kernel/parallel_compiler.ex:229: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.