Documentation
¶
Overview ¶
Package cid is an implementation of Content Identifiers, specifically the restricted DASL subset of CIDs.
Example ¶
package main
import (
"fmt"
"github.com/hyphacoop/go-dasl/cid"
)
func main() {
// Create a CID from a string representation
cidString := "bafkreifn5yxi7nkftsn46b6x26grda57ict7md2xuvfbsgkiahe2e7vnq4"
c1, err := cid.NewCidFromString(cidString)
if err != nil {
panic(err)
}
fmt.Printf("CID from string: %s\n", c1.String())
// Create a CID from binary bytes
cidBytes := []byte{0x01, 0x55, 0x12, 0x20, 0xad, 0xee, 0x2e, 0x8f, 0xb5, 0x45, 0x9c, 0x9b, 0xcf, 0x07, 0xd7, 0xd7, 0x8d, 0x11, 0x83, 0xbf, 0x40, 0xa7, 0xf6, 0x0f, 0x57, 0xa5, 0x4a, 0x19, 0x19, 0x48, 0x01, 0xc9, 0xa2, 0x7e, 0xad, 0x87}
c2, err := cid.NewCidFromBytes(cidBytes)
if err != nil {
panic(err)
}
fmt.Printf("CID from bytes: %s\n", c2.String())
// Both should be equal
fmt.Printf("CIDs are equal: %t\n", c1.Equal(c2))
// Show CID properties
fmt.Printf("Codec: %d\n", c1.Codec())
fmt.Printf("Hash type: %d\n", c1.HashType())
}
Output: CID from string: bafkreifn5yxi7nkftsn46b6x26grda57ict7md2xuvfbsgkiahe2e7vnq4 CID from bytes: bafkreifn5yxi7nkftsn46b6x26grda57ict7md2xuvfbsgkiahe2e7vnq4 CIDs are equal: true Codec: 85 Hash type: 18
Index ¶
- Constants
- Variables
- type Cid
- func HashBytes(b []byte) Cid
- func HashBytesBlake3(b []byte) Cid
- func HashReader(r io.Reader) (Cid, error)
- func HashReaderBlake3(r io.Reader) (Cid, error)
- func MustNewCidFromBytes(b []byte) Cid
- func MustNewCidFromString(s string) Cid
- func NewCidFromBytes(in []byte) (Cid, error)
- func NewCidFromInfo(codec Codec, hashType HashType, digest [HashSize]byte) (Cid, error)
- func NewCidFromReader(r ReadByteReader) (Cid, error)
- func NewCidFromString(s string) (Cid, error)
- func (c Cid) AppendBinary(b []byte) ([]byte, error)
- func (c Cid) Bytes() []byte
- func (c Cid) Codec() Codec
- func (c Cid) Defined() bool
- func (c Cid) Digest() [HashSize]byte
- func (c Cid) Equal(o Cid) bool
- func (c Cid) HashType() HashType
- func (c Cid) Hasher() hash.Hash
- func (c Cid) MarshalBinary() ([]byte, error)
- func (c Cid) MarshalCBOR() ([]byte, error)
- func (c Cid) MarshalJSON() ([]byte, error)
- func (c Cid) MarshalText() ([]byte, error)
- func (c Cid) String() string
- func (c *Cid) UnmarshalBinary(data []byte) error
- func (c *Cid) UnmarshalCBOR(b []byte) error
- func (c *Cid) UnmarshalJSON(data []byte) error
- func (c *Cid) UnmarshalText(text []byte) error
- func (c Cid) VerifyBytes(b []byte) bool
- func (c Cid) VerifyReader(r io.Reader) (bool, error)
- type Codec
- type ForbiddenCidError
- type HashType
- type RawCid
- type ReadByteReader
Examples ¶
Constants ¶
const ( // All DASL CIDs are 36 bytes long // 4 header bytes + 32 hash digest bytes CidBinaryLength = 36 // Encoded as a base32 string it is 58 bytes, plus the 'b' prefix. CidStrLength = 59 CidVersion = 0x01 CodecRaw Codec = 0x55 CodecDrisl Codec = 0x71 HashTypeSha256 HashType = 0x12 HashTypeBlake3 HashType = 0x1e // The hash digest length HashSize = 32 )
Variables ¶
var ( // EmptyCid is a raw SHA-256 CID that represents no data. // Its digest is the SHA-256 hash of the empty byte string "". // Try not to use it as a sentinel value. EmptyCid = Cid{/* contains filtered or unexported fields */} ErrUndefinedCid = errors.New("go-dasl/cid: cannot marshal nil Cid (use omitzero over omitempty)") )
Functions ¶
This section is empty.
Types ¶
type Cid ¶
type Cid struct {
// contains filtered or unexported fields
}
Cid is a DASL CID.
It is always valid, unless created directly such as cid.Cid{} or new(cid.Cid). That will cause invalid output if methods are called upon it, unless otherwise documented. Marshal methods like MarshalBinary can return errors, and so will check for this and return UndefinedCidError instead of panicing or encoding an invalid CID.
Programs using CIDs should typically store and pass them as values, not pointers. That is, CID variables and struct fields should be of type cid.Cid, not *cid.Cid.
If you want to omit empty CID values from being encoded, use the omitzero struct tag instead of omitempty.
func HashBytesBlake3 ¶ added in v0.8.0
HashBytesBlake3 creates a raw BLAKE3 CID by hashing the provided bytes.
func HashReader ¶
HashReader creates a raw SHA-256 CID by hashing all the data in the reader. Any error returned comes from the reader.
func HashReaderBlake3 ¶ added in v0.8.0
HashReaderBlake3 creates a raw Blake3 CID by hashing all the data in the reader. Any error returned comes from the reader.
func MustNewCidFromBytes ¶ added in v0.3.0
MustNewCidFromBytes calls NewCidFromBytes and panics if it returns an error.
func MustNewCidFromString ¶ added in v0.3.0
MustNewCidFromString calls NewCidFromString and panics if it returns an error.
func NewCidFromBytes ¶
NewCidFromBytes parses a binary DASL CID. A ForbiddenCidError is returned if it is invalid in any way.
Note this is not the same as the bytes for a CID encoded in DRISL (CBOR).
The input data is copied and so can be modified afterward.
func NewCidFromInfo ¶
NewCidFromInfo creates a DASL CID manually from the codec, hash type, and hash digest. An error is only returned if the codec or hashType provided don't conform to DASL.
func NewCidFromReader ¶
func NewCidFromReader(r ReadByteReader) (Cid, error)
NewCidFromReader parses a binary DASL CID from the given reader. A ForbiddenCidError is returned if it is invalid in any way. Extra data after the CID is allowed.
Note this is not the same as the bytes for a CID encoded in DRISL (CBOR).
If your reader does not support io.ByteReader, you can easily fulfill this interface by wrapping it with bufio.NewReader.
func NewCidFromString ¶
NewCidFromString parses a string DASL CID. A ForbiddenCidError is returned if it is invalid DASL.
func (Cid) AppendBinary ¶ added in v0.5.0
AppendBinary fulfills the encoding.BinaryAppender interface. It simply appends the Bytes() output.
func (Cid) Bytes ¶
Bytes returns the CID in binary format. It is safe to modify. It is always CidBinaryLength bytes long. Note this is not the representation used in DRISL (CBOR).
func (Cid) Defined ¶
Defined returns false if this Cid has no data due to being created with cid.Cid{} or new(cid.Cid). This method shouldn't be needed as long as you don't do this.
func (Cid) Equal ¶ added in v0.7.0
Equal returns true if the two CIDs are exactly the same.
CIDs with the same hash type and digest but different codecs are not considered equal.
This is equivalent to comparing the .Bytes() or .String() output of two CIDs, but more efficient.
func (Cid) Hasher ¶ added in v0.8.0
Hasher returns a new appropriate hash interface based on the hash type of the CID.
This could be used to hash some data and compare against the CID digest, although note VerifyBytes and VerifyReader already do that.
func (Cid) MarshalBinary ¶ added in v0.5.0
MarshalBinary fulfills the encoding.BinaryMarshaler interface. It is equivalent to Bytes().
func (Cid) MarshalCBOR ¶
MarshalCBOR fulfills the drisl.Marshaler interface.
func (Cid) MarshalJSON ¶ added in v0.5.0
MarshalJSON fulfills the json.Marshaler interface. It follows the ATproto standard: {"$link": "bafkr..."}
This is just for simple display purposes.
func (Cid) MarshalText ¶ added in v0.5.0
MarshalText fulfills the encoding.TextMarshaler interface. It is equivalent to String().
func (Cid) String ¶
String returns the CID in string format. It will always be CidStrLength bytes (or ASCII characters) long.
func (*Cid) UnmarshalBinary ¶ added in v0.5.0
UnmarshalBinary fulfills the encoding.BinaryUnmarshaler interface. It is equivalent to NewCidFromBytes.
func (*Cid) UnmarshalCBOR ¶
UnmarshalCBOR fulfills the drisl.Unmarshaler interface.
func (*Cid) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON fulfills the json.Unmarshaler interface. It follows the ATproto standard: {"$link": "bafkr..."}
func (*Cid) UnmarshalText ¶ added in v0.5.0
UnmarshalText fulfills the encoding.TextUnmarshaler interface. It is equivalent to NewCidFromString.
func (Cid) VerifyBytes ¶ added in v0.8.0
VerifyBytes checks whether the provided bytes match the hash digest in the CID. The codec information is not checked.
type ForbiddenCidError ¶
type ForbiddenCidError struct {
// contains filtered or unexported fields
}
ForbiddenCidError is returned if the CID is invalid in some way according to the DRISL spec.
func (*ForbiddenCidError) Error ¶
func (e *ForbiddenCidError) Error() string
type HashType ¶
type HashType byte
HashType is the algorithm of the hash digest embedded in the CID.
type RawCid ¶ added in v0.4.0
type RawCid []byte
RawCid is an unvalidated CID.
It is used to store CID information when decoding non-DASL CIDs from DRISL is enabled. You can also use it to encode non-DASL CIDs. Only do this if you are not working in a DASL-compliant ecosystem!
It holds the bytes of a binary CID. It is not the same bytes as CID-in-CBOR.
There are no guarantees this is a valid CID by any spec.
You can pass it to NewCidFromBytes to validate it as a DASL CID, or to the Cast function of the go-cid library to parse it as an original IPFS CID.
func (RawCid) MarshalCBOR ¶ added in v0.4.0
func (*RawCid) UnmarshalCBOR ¶ added in v0.4.0
type ReadByteReader ¶ added in v0.4.0
type ReadByteReader interface {
io.Reader
io.ByteReader
}