multihash

package
v0.0.0-...-c9e5fdf Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2014 License: MIT, MIT Imports: 7 Imported by: 0

README

go-multihash

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

Constants

View Source
const BLAKE2B = 0x40
View Source
const BLAKE2S = 0x41
View Source
const SHA1 = 0x11

constants

View Source
const SHA2_256 = 0x12
View Source
const SHA2_512 = 0x13
View Source
const SHA3 = 0x14

Variables

View Source
var Codes = map[int]string{
	0x11: "sha1",
	0x12: "sha2-256",
	0x13: "sha2-512",
	0x14: "sha3",
	0x40: "blake2b",
	0x41: "blake2s",
}
View Source
var DefaultLengths = map[int]int{
	0x11: 20,
	0x12: 32,
	0x13: 64,
	0x14: 64,
	0x40: 64,
	0x41: 32,
}
View Source
var Names = map[string]int{
	"sha1":     0x11,
	"sha2-256": 0x12,
	"sha2-512": 0x13,
	"sha3":     0x14,
	"blake2b":  0x40,
	"blake2s":  0x41,
}

Functions

func AppCode

func AppCode(code int) bool

Checks whether a multihash code is part of the App range.

func Encode

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

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

func ValidCode

func ValidCode(code int) bool

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)

Decodes a hash from the given Multihash.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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