├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── config └── config.exs ├── lib ├── multihash.ex └── util.ex ├── mix.exs ├── mix.lock └── test ├── multihash_test.exs ├── test_helper.exs └── util_test.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /deps 3 | /doc 4 | /docs 5 | erl_crash.dump 6 | *.ez 7 | 8 | .DS_Store 9 | .elixir_ls 10 | .vscode 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: elixir 2 | elixir: 1.2.6 3 | otp_release: 4 | - 18.2 5 | sudo: false 6 | before_script: 7 | - mix deps.get --only test 8 | script: 9 | - mix test 10 | after_script: 11 | - cd $TRAVIS_BUILD_DIR 12 | - mix deps.get --only docs 13 | - MIX_ENV=docs mix inch.report 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog for ex_multihash 2 | 3 | ## v2.0.0 4 | 5 | ### 1. Enhancements 6 | * [Code] Removed the dependency on `monad` library and used `with` expression instead. 7 | * [Mix] Changed the dependency of elixir to v1.2 as `with` clause was introduced in that version. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Zohaib Rauf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **is project is no longer maintained and has been archived.** 2 | 3 | ex_multihash 4 | ============ 5 | 6 | [![hex.pm version](https://img.shields.io/hexpm/v/httpotion.svg?style=flat-square)](https://hex.pm/packages/ex_multihash) 7 | [![API Docs](https://img.shields.io/badge/api-docs-yellow.svg?style=flat-square)](http://hexdocs.pm/ex_multihash/) 8 | [![Travis CI](https://img.shields.io/travis/multiformats/ex_multihash.svg?style=flat-square&branch=master)](https://travis-ci.org/multiformats/ex_multihash) 9 | [![Inline docs](http://inch-ci.org/github/multiformats/ex_multihash.svg)](http://inch-ci.org/github/multiformats/ex_multihash) 10 | [![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats) 11 | [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs) 12 | [![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) 13 | 14 | > Multihash implementation in Elixir 15 | 16 | This is the [Multihash](https://github.com/multiformats/multihash) implementation in Elixir. 17 | 18 | ## Table of Contents 19 | 20 | - [Install](#install) 21 | - [Usage](#usage) 22 | - [Encoding](#encoding) 23 | - [Examples](#examples) 24 | - [Examples](#examples-1) 25 | - [Examples](#examples-2) 26 | - [Decoding](#decoding) 27 | - [Examples](#examples-3) 28 | - [Examples](#examples-4) 29 | - [Maintainers](#maintainers) 30 | - [Contribute](#contribute) 31 | - [License](#license) 32 | 33 | ## Install 34 | 35 | To use ex_multihash add to your `mix.exs` file: 36 | 37 | ```elixir 38 | defp deps do 39 | [ 40 | {:ex_multihash, "~> 1.0"} 41 | ] 42 | end 43 | ``` 44 | 45 | ## Usage 46 | 47 | ### Encoding 48 | 49 | Encode the provided hashed `digest` to the provided multihash of `hash_code`. 50 | 51 | #### Examples 52 | 53 | ```elixir 54 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello")) 55 | {:ok, <<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>} 56 | 57 | iex> Multihash.encode(:sha2_256, :crypto.hash(:sha256, "Hello")) 58 | {: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>>} 59 | ``` 60 | 61 | Invalid `hash_code` or `digest` corresponding to the hash function will return an error. 62 | 63 | #### Examples 64 | 65 | ```elixir 66 | iex> Multihash.encode(:sha2_unknow, :crypto.hash(:sha, "Hello")) 67 | {:error, "Invalid hash function"} 68 | 69 | iex> Multihash.encode(0x20, :crypto.hash(:sha, "Hello")) 70 | {:error, "Invalid hash code"} 71 | ``` 72 | 73 | It's possible to [truncate a digest](https://github.com/multiformats/multihash/issues/1) by passing an optional `length` parameter. Passing a `length` longer than the default digest length of the hash function will return an error. 74 | 75 | #### Examples 76 | 77 | ```elixir 78 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 10) 79 | {:ok, <<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>} 80 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 30) 81 | {:error, "Invalid digest length"} 82 | ``` 83 | 84 | ### Decoding 85 | 86 | Decode the provided multihash to: 87 | 88 | ```elixir 89 | %Multihash{name: atom, code: integer, length: integer, digest: bitstring} 90 | ``` 91 | 92 | #### Examples 93 | 94 | ```elixir 95 | 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>>) 96 | {: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>>}} 97 | ``` 98 | 99 | Invalid multihash will result in errors 100 | 101 | #### Examples 102 | 103 | ```elixir 104 | iex> Multihash.decode(<<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171>>) 105 | {:error, "Invalid size"} 106 | 107 | 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>>) 108 | {:error, "Invalid digest length"} 109 | 110 | 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>>) 111 | {:error, "Invalid hash code"} 112 | 113 | 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>>) 114 | {:error, "Invalid length of provided hash function"} 115 | 116 | iex> Multihash.decode("Hello") 117 | {:error, "Invalid hash code"} 118 | ``` 119 | 120 | ## Maintainers 121 | 122 | Captain: [@zabirauf](https://github.com/zabirauf). 123 | 124 | ## Contribute 125 | 126 | Contributions welcome. Please check out [the issues](https://github.com/multiformats/ex_multihash/issues). 127 | 128 | Check out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) 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](https://github.com/ipfs/community/blob/master/code-of-conduct.md). 129 | 130 | Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. 131 | 132 | ## License 133 | [MIT](LICENSE) © 2015 Zohaib Rauf. 134 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for third- 9 | # party users, it should be done in your mix.exs file. 10 | 11 | # Sample configuration: 12 | # 13 | # config :logger, :console, 14 | # level: :info, 15 | # format: "$date $time [$level] $metadata$message\n", 16 | # metadata: [:user_id] 17 | 18 | # It is also possible to import configuration files, relative to this 19 | # directory. For example, you can emulate configuration per environment 20 | # by uncommenting the line below and defining dev.exs, test.exs and such. 21 | # Configuration from the imported file will override the ones defined 22 | # here (which is why it is important to import them last). 23 | # 24 | # import_config "#{Mix.env}.exs" 25 | -------------------------------------------------------------------------------- /lib/multihash.ex: -------------------------------------------------------------------------------- 1 | defmodule Multihash do 2 | @moduledoc """ 3 | Multihash library that follows jbenet multihash protocol so that the hash contains information 4 | about the hashing algorithm used making it more generic so that one can switch algorithm in future without 5 | much consequences 6 | """ 7 | 8 | @type t :: %Multihash{name: atom, code: integer, length: integer, digest: integer} 9 | defstruct name: :nil, code: 0, length: 0, digest: 0 10 | 11 | @type hash_type :: :sha1 | :sha2_256 | :sha2_512 | :sha3 | :blake2b | :blake2s 12 | 13 | @type error :: {:error, String.t} 14 | 15 | @type on_encode :: {:ok, binary} | error 16 | 17 | @type on_decode :: {:ok, t} | error 18 | 19 | @type integer_default :: integer | :default 20 | 21 | @hash_info %{ 22 | :sha1 => [code: 0x11, length: 20], 23 | :sha2_256 => [code: 0x12, length: 32], 24 | :sha2_512 => [code: 0x13, length: 64], 25 | :sha3 => [code: 0x14, length: 64], 26 | :blake2b => [code: 0x40, length: 64], 27 | :blake2s => [code: 0x41, length: 32] 28 | } 29 | 30 | @code_hash_map %{ 31 | 0x11 => :sha1, 32 | 0x12 => :sha2_256, 33 | 0x13 => :sha2_512, 34 | 0x14 => :sha3, 35 | 0x40 => :blake2b, 36 | 0x41 => :blake2s 37 | } 38 | 39 | 40 | # Error strings 41 | @error_invalid_digest_hash "Invalid digest or hash" 42 | @error_invalid_multihash "Invalid multihash" 43 | @error_invalid_length "Invalid length" 44 | @error_invalid_trunc_length "Invalid truncation length" 45 | @error_invalid_size "Invalid size" 46 | @error_invalid_hash_function "Invalid hash function" 47 | @error_invalid_hash_code "Invalid hash code" 48 | 49 | @doc ~S""" 50 | Encode the provided hashed `digest` to the provided multihash of `hash_code` 51 | 52 | ## Examples 53 | 54 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello")) 55 | {:ok, <<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>} 56 | 57 | iex> Multihash.encode(:sha3, "1234567890123456789012345678901234567890123456789012345678901234", 10) 58 | {:ok, <<20, 10, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48>>} 59 | 60 | 61 | iex> Multihash.encode(:sha2_256, :crypto.hash(:sha256, "Hello")) 62 | {: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>>} 63 | 64 | Invalid `hash_code`, `digest` length corresponding to the hash function will return an error 65 | 66 | iex> Multihash.encode(:sha2_unknow, :crypto.hash(:sha, "Hello")) 67 | {:error, "Invalid hash function"} 68 | 69 | iex> Multihash.encode(0x20, :crypto.hash(:sha, "Hello")) 70 | {:error, "Invalid hash code"} 71 | 72 | It's possible to [truncate a digest](https://github.com/jbenet/multihash/issues/1#issuecomment-91783612) 73 | by passing an optional `length` parameter. Passing a `length` longer than the default digest length 74 | of the hash function will return an error. 75 | 76 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 10) 77 | {:ok, <<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>} 78 | 79 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello") |> Kernel.binary_part(0, 10), 10) 80 | {:ok, <<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>} 81 | 82 | iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 30) 83 | {:error, "Invalid truncation length"} 84 | 85 | """ 86 | def encode(hash_code, digest, length \\ :default) 87 | 88 | @spec encode(integer, binary, integer_default) :: on_encode 89 | def encode(hash_code, digest, length) when is_number(hash_code) and is_binary(digest), do: 90 | encode(<>, digest, length) 91 | 92 | @spec encode(binary, binary, integer_default) :: on_encode 93 | def encode(<<_hash_code>> = hash_code, digest, length) when is_binary(digest) do 94 | with {:ok, function} <- get_hash_function(hash_code), 95 | do: encode(function, digest, length) 96 | end 97 | 98 | @spec encode(hash_type, binary, integer_default) :: on_encode 99 | def encode(hash_func, digest, length) when is_atom(hash_func) and is_binary(digest) do 100 | with {:ok, info} <- get_hash_info(hash_func), 101 | :ok <- check_digest_length(info, digest, length), 102 | do: encode_internal(info, digest, length) 103 | end 104 | 105 | def encode(_digest,_hash_code, _length), do: {:error, @error_invalid_digest_hash} 106 | 107 | @doc ~S""" 108 | Decode the provided multi hash to %Multihash{code: , name: , length: , digest: } 109 | 110 | ## Examples 111 | 112 | 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>>) 113 | {: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>>}} 114 | 115 | iex> Multihash.decode(<<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>) 116 | {:ok, %Multihash{name: :sha1, code: 17, length: 10, digest: <<247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>}} 117 | 118 | Invalid multihash will result in errors 119 | 120 | iex> Multihash.decode(<<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171>>) 121 | {:error, "Invalid size"} 122 | 123 | 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>>) 124 | {:error, "Invalid hash code"} 125 | 126 | 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>>) 127 | {:error, "Invalid length"} 128 | 129 | iex> Multihash.decode("Hello") 130 | {:error, "Invalid hash code"} 131 | 132 | """ 133 | @spec decode(binary) :: on_decode 134 | def decode(<>) do 135 | with {:ok, function} <- get_hash_function(<>), 136 | {:ok, info} <- get_hash_info(function), 137 | :ok <- check_length(info, length), 138 | :ok <- check_truncated_digest_length(info, digest, length), 139 | do: decode_internal(info, digest, length) 140 | end 141 | 142 | def decode(_), do: {:error, @error_invalid_multihash} 143 | 144 | @doc ~S""" 145 | Checks if the code is within application range 146 | 147 | ## Examples 148 | 149 | iex> Multihash.is_app_code(<<0x08>>) 150 | true 151 | 152 | iex> Multihash.is_app_code(<<0x10>>) 153 | false 154 | """ 155 | @spec is_app_code(<<_ :: 8 >>) :: boolean 156 | def is_app_code(<>), do: code >= 0 and code < 0x10 157 | 158 | @doc ~S""" 159 | Checks if the code is a valid code 160 | 161 | ## Examples 162 | 163 | iex> Multihash.is_valid_code(<<0x8>>) 164 | true 165 | 166 | iex> Multihash.is_valid_code(<<0x12>>) 167 | true 168 | 169 | iex> Multihash.is_valid_code(<<0x21>>) 170 | false 171 | """ 172 | @spec is_valid_code(<<_ :: 8 >>) :: boolean 173 | def is_valid_code(<<_>> = code) do 174 | if is_app_code(code) do 175 | true 176 | else 177 | is_valid_hash_code code 178 | end 179 | end 180 | 181 | @doc """ 182 | Checks if the `code` is a valid hash code 183 | """ 184 | defp is_valid_hash_code(<<_>> = code), do: is_valid_hash_code get_hash_function(code) 185 | defp is_valid_hash_code({:ok, _}), do: true 186 | defp is_valid_hash_code({:error, _}), do: false 187 | 188 | @doc """ 189 | Encode the `digest` to multihash, truncating it to the `trunc_length` if necessary 190 | """ 191 | defp encode_internal([code: code, length: length], <>, trunc_length) do 192 | case trunc_length do 193 | :default -> {:ok, <> <> digest} 194 | l when 0 < l and l <= length -> {:ok, <> <> Kernel.binary_part(digest, 0, l)} 195 | _ -> {:error, @error_invalid_trunc_length} 196 | end 197 | end 198 | 199 | @doc """ 200 | Decode the multihash to %Multihash{name, code, length, digest} structure 201 | """ 202 | defp decode_internal([code: code, length: _default_length], <>, length) do 203 | {:ok, name} = get_hash_function <> 204 | {:ok, 205 | %Multihash{ 206 | name: name, 207 | code: code, 208 | length: length, 209 | digest: digest}} 210 | end 211 | 212 | @doc """ 213 | Checks if the incoming multihash has a `length` field equal or lower than the `default_length` of the hash function 214 | """ 215 | defp check_length([code: _code, length: default_length], original_length) do 216 | case original_length do 217 | l when 0 < l and l <= default_length -> :ok 218 | _ -> {:error, @error_invalid_length} 219 | end 220 | end 221 | 222 | @doc """ 223 | Checks if the incoming multihash has a `length` field fitting the actual size of the possibly truncated `digest` 224 | """ 225 | defp check_truncated_digest_length([code: _code, length: _default_length], digest, length) when is_binary(digest) do 226 | case byte_size(digest) do 227 | ^length -> :ok 228 | _ -> {:error, @error_invalid_size} 229 | end 230 | end 231 | 232 | @doc """ 233 | Checks if the length of the `digest` is the default length for the hash function,or at least greater than the the desired truncated length 234 | """ 235 | defp check_digest_length([code: _code, length: default_length], digest, trunc_length) when is_binary(digest) do 236 | case byte_size(digest) do 237 | ^default_length -> :ok 238 | digest_len when trunc_length != :default and digest_len >= trunc_length -> :ok 239 | _ -> {:error, @error_invalid_size} 240 | end 241 | end 242 | 243 | @doc """ 244 | Get hash info from the @hash_info keyword map based on the provided `hash_func` 245 | """ 246 | defp get_hash_info(hash_func) when is_atom(hash_func), do: 247 | get_from_dict(@hash_info, hash_func, @error_invalid_hash_function) 248 | 249 | @doc """ 250 | Get hash function from the @code_hash_map based on the `code` key 251 | """ 252 | defp get_hash_function(<>), do: 253 | get_from_dict(@code_hash_map, code, @error_invalid_hash_code) 254 | 255 | @doc """ 256 | Generic function that retrieves a key from the dictionary and if the key is not there then returns {:error, `failure_message`} 257 | """ 258 | defp get_from_dict(dict, key, failure_message) do 259 | case Map.get(dict, key, :none) do 260 | :none -> {:error, failure_message} 261 | value-> {:ok, value} 262 | end 263 | end 264 | 265 | end 266 | -------------------------------------------------------------------------------- /lib/util.ex: -------------------------------------------------------------------------------- 1 | defmodule Multihash.Util do 2 | @moduledoc """ 3 | Utility functions to create multihash from data 4 | """ 5 | 6 | @doc """ 7 | Creates a multihash from the data provided. The hash options are 8 | * :sha1 9 | * :sha2_256 10 | * :sha2_512 11 | 12 | ## Examples 13 | 14 | iex> Multihash.Util.sum("Hello", :sha1) 15 | <<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>> 16 | 17 | iex> Multihash.Util.sum("Hello", :sha2_256) 18 | <<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>> 19 | 20 | iex> Multihash.Util.sum("Hello", :sha2_512) 21 | <<19, 64, 54, 21, 248, 12, 157, 41, 62, 215, 64, 38, 135, 249, 75, 34, 213, 142, 82, 155, 140, 199, 145, 111, 143, 172, 127, 221, 247, 251, 213, 175, 76, 247, 119, 211, 215, 149, 167, 160, 10, 22, 191, 126, 127, 63, 185, 86, 30, 233, 186, 174, 72, 13, 169, 254, 122, 24, 118, 158, 113, 136, 107, 3, 243, 21>> 22 | 23 | """ 24 | @spec sum(binary, Multihash.hash_type) :: binary 25 | def sum(data, :sha1), do: :crypto.hash(:sha, data) |> create_multihash(:sha1) 26 | def sum(data, :sha2_256), do: :crypto.hash(:sha256, data) |> create_multihash(:sha2_256) 27 | def sum(data, :sha2_512), do: :crypto.hash(:sha512, data) |> create_multihash(:sha2_512) 28 | 29 | @spec create_multihash(binary, Multihash.hash_type) :: binary 30 | defp create_multihash(digest, hash) do 31 | {:ok, multihash} = Multihash.encode(hash, digest) 32 | multihash 33 | end 34 | 35 | end 36 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Multihash.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [app: :ex_multihash, 6 | version: "2.0.0", 7 | elixir: "~> 1.2", 8 | description: description(), 9 | build_embedded: Mix.env == :prod, 10 | start_permanent: Mix.env == :prod, 11 | package: package(), 12 | deps: deps()] 13 | end 14 | 15 | def description do 16 | "This library is the Multihash implementation in Elixir" 17 | end 18 | 19 | def package do 20 | [ 21 | licenses: ["MIT License"], 22 | maintainers: ["Zohaib Rauf", "Multiformat Organization"], 23 | links: %{ 24 | "Github" => "https://github.com/multiformats/ex_multihash", 25 | "Docs" => "https://hexdocs.pm/ex_multihash" 26 | } 27 | ] 28 | end 29 | 30 | # Configuration for the OTP application 31 | # 32 | # Type `mix help compile.app` for more information 33 | def application do 34 | [applications: [:logger]] 35 | end 36 | 37 | # Dependencies can be Hex packages: 38 | # 39 | # {:mydep, "~> 0.3.0"} 40 | # 41 | # Or git/path repositories: 42 | # 43 | # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} 44 | # 45 | # Type `mix help deps` for more examples and options 46 | defp deps do 47 | [ 48 | {:inch_ex, "~> 0.5", only: :docs}, 49 | {:dialyxir, "~> 0.3.5", only: :dev}, 50 | {:ex_doc, "~> 0.12", only: :dev} 51 | ] 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{"dialyxir": {:hex, :dialyxir, "0.3.5", "eaba092549e044c76f83165978979f60110dc58dd5b92fd952bf2312f64e9b14", [:mix], []}, 2 | "earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], []}, 3 | "ex_doc": {:hex, :ex_doc, "0.13.0", "aa2f8fe4c6136a2f7cfc0a7e06805f82530e91df00e2bff4b4362002b43ada65", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]}, 4 | "inch_ex": {:hex, :inch_ex, "0.5.3", "39f11e96181ab7edc9c508a836b33b5d9a8ec0859f56886852db3d5708889ae7", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]}, 5 | "monad": {:hex, :monad, "1.0.4", "21504cdb9986cac3599055997dc984e77dbb89aef71decadf4921cf0e8f28c06", [:mix], []}, 6 | "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []}} 7 | -------------------------------------------------------------------------------- /test/multihash_test.exs: -------------------------------------------------------------------------------- 1 | defmodule MultihashTest do 2 | use ExUnit.Case, async: true 3 | doctest Multihash 4 | 5 | end 6 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /test/util_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Multihash.UtilTest do 2 | use ExUnit.Case, async: true 3 | doctest Multihash.Util 4 | 5 | end 6 | --------------------------------------------------------------------------------