multihash

package
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2016 License: MIT, MIT Imports: 9 Imported by: 0

README

go-multihash

travis

multihash implementation in Go.

Example

package main

import (
  "encoding/hex"
  "fmt"
  "github.com/jbenet/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

License

MIT

Documentation

Index

Examples

Constants

View Source
const (
	SHA1     = 0x11
	SHA2_256 = 0x12
	SHA2_512 = 0x13
	SHA3     = 0x14
	BLAKE2B  = 0x40
	BLAKE2S  = 0x41
)

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")
)

errors

View Source
var Codes = map[int]string{
	SHA1:     "sha1",
	SHA2_256: "sha2-256",
	SHA2_512: "sha2-512",
	SHA3:     "sha3",
	BLAKE2B:  "blake2b",
	BLAKE2S:  "blake2s",
}

Codes maps a hash code to it's name

View Source
var DefaultLengths = map[int]int{
	SHA1:     20,
	SHA2_256: 32,
	SHA2_512: 64,
	SHA3:     64,
	BLAKE2B:  64,
	BLAKE2S:  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]int{
	"sha1":     SHA1,
	"sha2-256": SHA2_256,
	"sha2-512": SHA2_512,
	"sha3":     SHA3,
	"blake2b":  BLAKE2B,
	"blake2s":  BLAKE2S,
}

Names maps the name of a hash to the code

Functions

func AppCode

func AppCode(code int) bool

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

func Encode

func Encode(buf []byte, code int) ([]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 int) bool

ValidCode checks whether a multihash code is valid.

Types

type DecodedMultihash

type DecodedMultihash struct {
	Code   int
	Name   string
	Length int
	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 int, 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