README.md
1# go-multihash
2
3[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
4[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
5[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
6[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
7[![GoDoc](https://godoc.org/github.com/multiformats/go-multihash?status.svg)](https://godoc.org/github.com/multiformats/go-multihash)
8[![Travis CI](https://img.shields.io/travis/multiformats/go-multihash.svg?style=flat-square&branch=master)](https://travis-ci.org/multiformats/go-multihash)
9[![codecov.io](https://img.shields.io/codecov/c/github/multiformats/go-multihash.svg?style=flat-square&branch=master)](https://codecov.io/github/multiformats/go-multihash?branch=master)
10
11> [multihash](https://github.com/multiformats/multihash) implementation in Go
12
13## Table of Contents
14
15- [Install](#install)
16- [Usage](#usage)
17- [Maintainers](#maintainers)
18- [Contribute](#contribute)
19- [License](#license)
20
21## Install
22
23`go-multihash` is a standard Go module which can be installed with:
24
25```sh
26go get github.com/multiformats/go-multihash
27```
28
29## Usage
30
31
32### Example
33
34This example takes a standard hex-encoded data and uses `EncodeName` to calculate the SHA1 multihash value for the buffer.
35
36The resulting hex-encoded data corresponds to: `<hash function code><digest size><hash function output>`, which could be re-parsed
37with `Multihash.FromHexString()`.
38
39
40```go
41package main
42
43import (
44 "encoding/hex"
45 "fmt"
46
47 "github.com/multiformats/go-multihash"
48)
49
50func main() {
51 // ignores errors for simplicity.
52 // don't do that at home.
53 // Decode a SHA1 hash to a binary buffer
54 buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
55
56 // Create a new multihash with it.
57 mHashBuf, _ := multihash.EncodeName(buf, "sha1")
58 // Print the multihash as hex string
59 fmt.Printf("hex: %s\n", hex.EncodeToString(mHashBuf))
60
61 // Parse the binary multihash to a DecodedMultihash
62 mHash, _ := multihash.Decode(mHashBuf)
63 // Convert the sha1 value to hex string
64 sha1hex := hex.EncodeToString(mHash.Digest)
65 // Print all the information in the multihash
66 fmt.Printf("obj: %v 0x%x %d %s\n", mHash.Name, mHash.Code, mHash.Length, sha1hex)
67}
68```
69
70To run, copy to [example/foo.go](example/foo.go) and:
71
72```
73> cd example/
74> go build
75> ./example
76hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
77obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
78```
79
80## Contribute
81
82Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multihash/issues).
83
84Check 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).
85
86Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
87
88## License
89
90[MIT](LICENSE) © 2014 Juan Batiz-Benet
91