README.md 2.66 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2
go-cid
==================
tavit ohanian's avatar
tavit ohanian committed
3

Jeromy's avatar
Jeromy committed
4 5 6
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
7 8
[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![GoDoc](https://godoc.org/github.com/ipfs/go-cid?status.svg)](https://godoc.org/github.com/ipfs/go-cid)
Jeromy's avatar
Jeromy committed
9 10 11
[![Coverage Status](https://coveralls.io/repos/github/ipfs/go-cid/badge.svg?branch=master)](https://coveralls.io/github/ipfs/go-cid?branch=master)
[![Travis CI](https://travis-ci.org/ipfs/go-cid.svg?branch=master)](https://travis-ci.org/ipfs/go-cid)

12
> A package to handle content IDs in Go.
Jeromy's avatar
Jeromy committed
13

14 15
This is an implementation in Go of the [CID spec](https://github.com/ipld/cid).
It is used in `go-ipfs` and related packages to refer to a typed hunk of data.
Jeromy's avatar
Jeromy committed
16

Steven Allen's avatar
Steven Allen committed
17 18 19
## Lead Maintainer

[Eric Myhre](https://github.com/warpfork)
Jeromy's avatar
Jeromy committed
20 21 22 23 24 25 26 27 28 29 30

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)

## Install

31 32
`go-cid` is a standard Go module which can be installed with:

Jeromy's avatar
Jeromy committed
33
```sh
34
go get github.com/ipfs/go-cid
Jeromy's avatar
Jeromy committed
35 36
```

37 38 39 40
## Usage

### Running tests

b5's avatar
b5 committed
41
Run tests with `go test` from the directory root
42 43

```sh
b5's avatar
b5 committed
44
go test
45 46 47 48 49
```

### Examples

#### Parsing string input from users
Jeromy's avatar
Jeromy committed
50

Jeromy's avatar
Jeromy committed
51
```go
Jeromy's avatar
Jeromy committed
52
// Create a cid from a marshaled string
Steven Allen's avatar
Steven Allen committed
53
c, err := cid.Decode("bafzbeigai3eoy2ccc7ybwjfz5r3rdxqrinwi4rwytly24tdbh6yk7zslrm")
Jeromy's avatar
Jeromy committed
54 55
if err != nil {...}

Jeromy's avatar
Jeromy committed
56 57
fmt.Println("Got CID: ", c)
```
Jeromy's avatar
Jeromy committed
58

59 60
#### Creating a CID from scratch

Jeromy's avatar
Jeromy committed
61
```go
Jeromy's avatar
Jeromy committed
62 63 64 65 66 67 68 69 70 71 72 73
// Create a cid manually by specifying the 'prefix' parameters
pref := cid.Prefix{
	Version: 1,
	Codec: cid.Raw,
	MhType: mh.SHA2_256,
	MhLength: -1, // default length
}

// And then feed it some data
c, err := pref.Sum([]byte("Hello World!"))
if err != nil {...}

Jeromy's avatar
Jeromy committed
74 75
fmt.Println("Created CID: ", c)
```
Jeromy's avatar
Jeromy committed
76

77 78
#### Check if two CIDs match

Jeromy's avatar
Jeromy committed
79
```go
Jeromy's avatar
Jeromy committed
80 81 82 83
// To test if two cid's are equivalent, be sure to use the 'Equals' method:
if c1.Equals(c2) {
	fmt.Println("These two refer to the same exact data!")
}
Jeromy's avatar
Jeromy committed
84
```
Jeromy's avatar
Jeromy committed
85

86 87
#### Check if some data matches a given CID

Jeromy's avatar
Jeromy committed
88
```go
89
// To check if some data matches a given cid,
Jeromy's avatar
Jeromy committed
90
// Get your CIDs prefix, and use that to sum the data in question:
Jeromy's avatar
Jeromy committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
other, err := c.Prefix().Sum(mydata)
if err != nil {...}

if !c.Equals(other) {
	fmt.Println("This data is different.")
}

```

## Contribute

PRs are welcome!

Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.

## License

MIT © Jeromy Johnson