multihash

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2017 License: MIT Imports: 13 Imported by: 0

README

go-multihash

Travis CI codecov.io

multihash implementation in Go

Table of Contents

Install

go get github.com/multiformats/go-multihash

Usage

package main

import (
  "encoding/hex"
  "fmt"
  "github.com/multiformats/go-multihash"
)

func main() {
  // ignores errors for simplicity.
  // don't do that at home.

  buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
  mhbuf, _ := multihash.EncodeName(buf, "sha1");
  mhhex := hex.EncodeToString(mhbuf)
  fmt.Printf("hex: %v\n", mhhex);

  o, _ := multihash.Decode(mhbuf);
  mhhex = hex.EncodeToString(o.Digest);
  fmt.Printf("obj: %v 0x%x %d %s\n", o.Name, o.Code, o.Length, mhhex);
}

Run test/foo.go

> cd test/
> go build
> ./test
hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33

Maintainers

Captain: @Kubuxu.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document 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.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2014 Juan Batiz-Benet

Documentation

Index

Examples

Constants

View Source
const (
	SHA1     = 0x11
	SHA2_256 = 0x12
	SHA2_512 = 0x13
	SHA3     = 0x14

	BLAKE2B_MIN = 0xb201
	BLAKE2B_MAX = 0xb240
	BLAKE2S_MIN = 0xb241
	BLAKE2S_MAX = 0xb260

	DBL_SHA2_256 = 0x56
)

constants

Variables

View Source
var (
	ErrUnknownCode      = errors.New("unknown multihash code")
	ErrTooShort         = errors.New("multihash too short. must be > 3 bytes")
	ErrTooLong          = errors.New("multihash too long. must be < 129 bytes")
	ErrLenNotSupported  = errors.New("multihash does not yet support digests longer than 127 bytes")
	ErrInvalidMultihash = errors.New("input isn't valid multihash")

	ErrVarintBufferShort = errors.New("uvarint: buffer too small")
	ErrVarintTooLong     = errors.New("uvarint: varint too big (max 64bit)")
)

errors

View Source
var Codes = map[uint64]string{
	SHA1:         "sha1",
	SHA2_256:     "sha2-256",
	SHA2_512:     "sha2-512",
	SHA3:         "sha3",
	DBL_SHA2_256: "dbl-sha2-256",
}

Codes maps a hash code to it's name

View Source
var DefaultLengths = map[uint64]int{
	SHA1:         20,
	SHA2_256:     32,
	SHA2_512:     64,
	SHA3:         64,
	DBL_SHA2_256: 32,
}

DefaultLengths maps a hash code to it's default length

View Source
var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
View Source
var Names = map[string]uint64{
	"sha1":         SHA1,
	"sha2-256":     SHA2_256,
	"sha2-512":     SHA2_512,
	"sha3":         SHA3,
	"dbl-sha2-256": DBL_SHA2_256,
}

Names maps the name of a hash to the code

Functions

func AppCode

func AppCode(code uint64) bool

AppCode checks whether a multihash code is part of the App range.

func Encode

func Encode(buf []byte, code uint64) ([]byte, error)

Encode a hash digest along with the specified function code. Note: the length is derived from the length of the digest itself.

func EncodeName

func EncodeName(buf []byte, name string) ([]byte, error)
Example
// ignores errors for simplicity - don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := EncodeName(buf, "sha1")
mhhex := hex.EncodeToString(mhbuf)
fmt.Printf("hex: %v\n", mhhex)
Output:

hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33

func ValidCode

func ValidCode(code uint64) bool

ValidCode checks whether a multihash code is valid.

Types

type DecodedMultihash

type DecodedMultihash struct {
	Code   uint64
	Name   string
	Length int // Length is just int as it is type of len() opearator
	Digest []byte
}

func Decode

func Decode(buf []byte) (*DecodedMultihash, error)

Decode a hash from the given Multihash.

Example
// ignores errors for simplicity - don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := EncodeName(buf, "sha1")
o, _ := Decode(mhbuf)
mhhex := hex.EncodeToString(o.Digest)
fmt.Printf("obj: %v 0x%x %d %s\n", o.Name, o.Code, o.Length, mhhex)
Output:

obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33

type ErrInconsistentLen

type ErrInconsistentLen struct {
	// contains filtered or unexported fields
}

ErrInconsistentLen is returned when a decoded multihash has an inconsistent length

func (ErrInconsistentLen) Error

func (e ErrInconsistentLen) Error() string

type Multihash

type Multihash []byte

func Cast

func Cast(buf []byte) (Multihash, error)

func FromB58String

func FromB58String(s string) (m Multihash, err error)

func FromHexString

func FromHexString(s string) (Multihash, error)

func Sum

func Sum(data []byte, code uint64, length int) (Multihash, error)

func (Multihash) B58String

func (m Multihash) B58String() string

func (*Multihash) HexString

func (m *Multihash) HexString() string

func (*Multihash) String

func (m *Multihash) String() string

type Reader

type Reader interface {
	io.Reader

	ReadMultihash() (Multihash, error)
}

Reader is an io.Reader wrapper that exposes a function to read a whole multihash, parse it, and return it.

func NewReader

func NewReader(r io.Reader) Reader

NewReader wraps an io.Reader with a multihash.Reader

type Writer

type Writer interface {
	io.Writer

	WriteMultihash(Multihash) error
}

Writer is an io.Writer wrapper that exposes a function to write a whole multihash.

func NewWriter

func NewWriter(w io.Writer) Writer

NewWriter wraps an io.Writer with a multihash.Writer

Directories

Path Synopsis
Package opts helps to write commands which may take multihash options.
Package opts helps to write commands which may take multihash options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL