Categorygithub.com/multiformats/go-multihash
modulepackage
0.2.3
Repository: https://github.com/multiformats/go-multihash.git
Documentation: pkg.go.dev

# README

go-multihash

GoDoc Travis CI codecov.io

multihash implementation in Go

Table of Contents

Install

go-multihash is a standard Go module which can be installed with:

go get github.com/multiformats/go-multihash

Usage

Example

This example takes a standard hex-encoded data and uses EncodeName to calculate the SHA1 multihash value for the buffer.

The resulting hex-encoded data corresponds to: <hash function code><digest size><hash function output>, which could be re-parsed with Multihash.FromHexString().

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/multiformats/go-multihash"
)

func main() {
	// ignores errors for simplicity.
	// don't do that at home.
	// Decode a SHA1 hash to a binary buffer
	buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")

	// Create a new multihash with it.
	mHashBuf, _ := multihash.EncodeName(buf, "sha1")
	// Print the multihash as hex string
	fmt.Printf("hex: %s\n", hex.EncodeToString(mHashBuf))

	// Parse the binary multihash to a DecodedMultihash
	mHash, _ := multihash.Decode(mHashBuf)
	// Convert the sha1 value to hex string
	sha1hex := hex.EncodeToString(mHash.Digest)
	// Print all the information in the multihash
	fmt.Printf("obj: %v 0x%x %d %s\n", mHash.Name, mHash.Code, mHash.Length, sha1hex)
}

To run, copy to example/foo.go and:

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

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

# Packages

No description provided by the author
No description provided by the author
Package opts helps to write commands which may take multihash options.
No description provided by the author
No description provided by the author

# Functions

Cast casts a buffer onto a multihash, and returns an error if it does not work.
Decode parses multihash bytes into a DecodedMultihash.
Encode a hash digest along with the specified function code.
EncodeName is like Encode() but providing a string name instead of a numeric code.
FromB58String parses a B58-encoded multihash.
FromHexString parses a hex-encoded multihash.
Register is an alias for Register in the core package.
MHFromBytes reads a multihash from the given byte buffer, returning the number of bytes read as well as the multihash.
NewReader wraps an io.Reader with a multihash.Reader.
NewSet creates a new set correctly initialized.
NewWriter wraps an io.Writer with a multihash.Writer.
Register is an alias for Register in the core package.
Sum obtains the cryptographic sum of a given buffer.
SumStream obtains the cryptographic sum of a given stream.

# Constants

constants.
constants.
constants.
constants.
constants.
constants.
Deprecated: use IDENTITY.
constants.
constants.
constants.
constants.
constants.
constants.
Deprecated: use MURMUR3X64_64.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.
constants.

# Variables

Codes maps a hash code to it's name.
DefaultLengths maps a multihash indicator code to the output size for that hash, in units of bytes.
errors.
errors.
No description provided by the author
ErrSumNotSupported is returned when the Sum function code is not implemented.
errors.
errors.
errors.
errors.
errors.
Names maps the name of a hash to the code.

# Structs

DecodedMultihash represents a parsed multihash and allows easy access to the different parts of a multihash.
ErrInconsistentLen is returned when a decoded multihash has an inconsistent length.
Set is a set of Multihashes, holding one copy per Multihash.

# Interfaces

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

# Type aliases

Multihash is byte slice with the following form: <hash function code><digest size><hash function output>.