Skip to content

Commit 54d61d0

Browse files
authored
Merge pull request #50 from multiformats/docs-improvements
README/golint: improve example, link to godoc, make golint happy
2 parents 78c341a + 514c672 commit 54d61d0

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

README.md

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
55
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
66
[![](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)
78
[![Travis CI](https://img.shields.io/travis/multiformats/go-multihash.svg?style=flat-square&branch=master)](https://travis-ci.org/multiformats/go-multihash)
89
[![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)
910

@@ -19,42 +20,76 @@
1920

2021
## Install
2122

23+
`go-multihash` is a standard Go module which can be installed with:
24+
2225
```sh
2326
go get github.com/multiformats/go-multihash
2427
```
2528

29+
Note that `go-multihash` is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
30+
2631
## Usage
2732

33+
### Using Gx and Gx-go
34+
35+
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project it is recommended that you:
36+
37+
```sh
38+
go get -u github.com/whyrusleeping/gx
39+
go get -u github.com/whyrusleeping/gx-go
40+
cd <your-project-repository>
41+
gx init
42+
gx import https://github.com/multiformats/go-multistream
43+
gx install --global
44+
gx-go --rewrite
45+
```
46+
47+
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
48+
49+
### Example
50+
51+
This example takes a standard hex-encoded data and uses `EncodeName` to calculate the SHA1 multihash value for the buffer.
52+
53+
The resulting hex-encoded data corresponds to: `<hash function code><digest size><hash function output>`, which could be re-parsed
54+
with `Multihash.FromHexString()`.
55+
56+
2857
```go
2958
package main
3059

3160
import (
32-
"encoding/hex"
33-
"fmt"
34-
"github.com/multiformats/go-multihash"
61+
"encoding/hex"
62+
"fmt"
63+
64+
"github.com/multiformats/go-multihash"
3565
)
3666

3767
func main() {
38-
// ignores errors for simplicity.
39-
// don't do that at home.
40-
41-
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
42-
mhbuf, _ := multihash.EncodeName(buf, "sha1");
43-
mhhex := hex.EncodeToString(mhbuf)
44-
fmt.Printf("hex: %v\n", mhhex);
45-
46-
o, _ := multihash.Decode(mhbuf);
47-
mhhex = hex.EncodeToString(o.Digest);
48-
fmt.Printf("obj: %v 0x%x %d %s\n", o.Name, o.Code, o.Length, mhhex);
68+
// ignores errors for simplicity.
69+
// don't do that at home.
70+
// Decode a SHA1 hash to a binary buffer
71+
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
72+
73+
// Create a new multihash with it.
74+
mHashBuf, _ := multihash.EncodeName(buf, "sha1")
75+
// Print the multihash as hex string
76+
fmt.Printf("hex: %s\n", hex.EncodeToString(mHashBuf))
77+
78+
// Parse the binary multihash to a DecodedMultihash
79+
mHash, _ := multihash.Decode(mHashBuf)
80+
// Convert the sha1 value to hex string
81+
sha1hex := hex.EncodeToString(mHash.Digest)
82+
// Print all the information in the multihash
83+
fmt.Printf("obj: %v 0x%x %d %s\n", mHash.Name, mHash.Code, mHash.Length, sha1hex)
4984
}
5085
```
5186

52-
Run [test/foo.go](test/foo.go)
87+
To run, copy to [example/foo.go](example/foo.go) and:
5388

5489
```
55-
> cd test/
90+
> cd example/
5691
> go build
57-
> ./test
92+
> ./example
5893
hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
5994
obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
6095
```

multihash.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package multihash is the Go implementation of
2+
// https://github.com/multiformats/multihash, or self-describing
3+
// hashes.
14
package multihash
25

36
import (
@@ -126,23 +129,31 @@ func uvarint(buf []byte) (uint64, []byte, error) {
126129
}
127130
}
128131

132+
// DecodedMultihash represents a parsed multihash and allows
133+
// easy access to the different parts of a multihash.
129134
type DecodedMultihash struct {
130135
Code uint64
131136
Name string
132-
Length int // Length is just int as it is type of len() opearator
133-
Digest []byte
137+
Length int // Length is just int as it is type of len() opearator
138+
Digest []byte // Digest holds the raw multihash bytes
134139
}
135140

141+
// Multihash is byte slice with the following form:
142+
// <hash function code><digest size><hash function output>.
143+
// See the spec for more information.
136144
type Multihash []byte
137145

146+
// HexString returns the hex-encoded representation of a multihash.
138147
func (m *Multihash) HexString() string {
139148
return hex.EncodeToString([]byte(*m))
140149
}
141150

151+
// String is an alias to HexString().
142152
func (m *Multihash) String() string {
143153
return m.HexString()
144154
}
145155

156+
// FromHexString parses a hex-encoded multihash.
146157
func FromHexString(s string) (Multihash, error) {
147158
b, err := hex.DecodeString(s)
148159
if err != nil {
@@ -152,10 +163,12 @@ func FromHexString(s string) (Multihash, error) {
152163
return Cast(b)
153164
}
154165

166+
// B58String returns the B58-encoded representation of a multihash.
155167
func (m Multihash) B58String() string {
156168
return b58.Encode([]byte(m))
157169
}
158170

171+
// FromB58String parses a B58-encoded multihash.
159172
func FromB58String(s string) (m Multihash, err error) {
160173
// panic handler, in case we try accessing bytes incorrectly.
161174
defer func() {
@@ -174,6 +187,8 @@ func FromB58String(s string) (m Multihash, err error) {
174187
return Cast(b)
175188
}
176189

190+
// Cast casts a buffer onto a multihash, and returns an error
191+
// if it does not work.
177192
func Cast(buf []byte) (Multihash, error) {
178193
dm, err := Decode(buf)
179194
if err != nil {
@@ -187,7 +202,7 @@ func Cast(buf []byte) (Multihash, error) {
187202
return Multihash(buf), nil
188203
}
189204

190-
// Decode a hash from the given Multihash.
205+
// Decode parses multihash bytes into a DecodedMultihash.
191206
func Decode(buf []byte) (*DecodedMultihash, error) {
192207

193208
if len(buf) < 3 {
@@ -242,6 +257,8 @@ func Encode(buf []byte, code uint64) ([]byte, error) {
242257
return append(start[:n], buf...), nil
243258
}
244259

260+
// EncodeName is like Encode() but providing a string name
261+
// instead of a numeric code. See Names for allowed values.
245262
func EncodeName(buf []byte, name string) ([]byte, error) {
246263
return Encode(buf, Names[name])
247264
}

sum.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ import (
1414
keccak "leb.io/hashland/keccakpg"
1515
)
1616

17+
// ErrSumNotSupported is returned when the Sum function code is not implemented
1718
var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
1819

20+
// Sum obtains the cryptographic sum of a given buffer. The length parameter
21+
// indicates the length of the resulting digest and passing a negative value
22+
// use default length values for the selected hash function.
1923
func Sum(data []byte, code uint64, length int) (Multihash, error) {
2024
m := Multihash{}
2125
err := error(nil)

0 commit comments

Comments
 (0)