|
4 | 4 | [](https://github.com/multiformats/multiformats) |
5 | 5 | [](https://webchat.freenode.net/?channels=%23ipfs) |
6 | 6 | [](https://github.com/RichardLitt/standard-readme) |
| 7 | +[](https://godoc.org/github.com/multiformats/go-multihash) |
7 | 8 | [](https://travis-ci.org/multiformats/go-multihash) |
8 | 9 | [](https://codecov.io/github/multiformats/go-multihash?branch=master) |
9 | 10 |
|
|
19 | 20 |
|
20 | 21 | ## Install |
21 | 22 |
|
| 23 | +`go-multihash` is a standard Go module which can be installed with: |
| 24 | + |
22 | 25 | ```sh |
23 | 26 | go get github.com/multiformats/go-multihash |
24 | 27 | ``` |
25 | 28 |
|
| 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 | + |
26 | 31 | ## Usage |
27 | 32 |
|
| 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 | + |
28 | 57 | ```go |
29 | 58 | package main |
30 | 59 |
|
31 | 60 | import ( |
32 | | - "encoding/hex" |
33 | | - "fmt" |
34 | | - "github.com/multiformats/go-multihash" |
| 61 | + "encoding/hex" |
| 62 | + "fmt" |
| 63 | + |
| 64 | + "github.com/multiformats/go-multihash" |
35 | 65 | ) |
36 | 66 |
|
37 | 67 | 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) |
49 | 84 | } |
50 | 85 | ``` |
51 | 86 |
|
52 | | -Run [test/foo.go](test/foo.go) |
| 87 | +To run, copy to [example/foo.go](example/foo.go) and: |
53 | 88 |
|
54 | 89 | ``` |
55 | | -> cd test/ |
| 90 | +> cd example/ |
56 | 91 | > go build |
57 | | -> ./test |
| 92 | +> ./example |
58 | 93 | hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 |
59 | 94 | obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 |
60 | 95 | ``` |
|
0 commit comments