multihash

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 8 Imported by: 27

Documentation

Index

Constants

View Source
const (
	IDENTITY      = 0x00
	SHA1          = 0x11
	SHA2_224      = 0x1013
	SHA2_256      = 0x12
	SHA2_384      = 0x20
	SHA2_512      = 0x13
	SHA2_512_224  = 0x1014
	SHA2_512_256  = 0x1015
	SHA3_224      = 0x17
	SHA3_256      = 0x16
	SHA3_384      = 0x15
	SHA3_512      = 0x14
	KECCAK_224    = 0x1A
	KECCAK_256    = 0x1B
	KECCAK_384    = 0x1C
	KECCAK_512    = 0x1D
	BLAKE3        = 0x1E
	SHAKE_128     = 0x18
	SHAKE_256     = 0x19
	MURMUR3X64_64 = 0x22
	MD5           = 0xd5
	DBL_SHA2_256  = 0x56
)

constants

Variables

View Source
var DefaultLengths = map[uint64]int{}

DefaultLengths maps a multihash indicator code to the output size for that hash, in units of bytes.

This map is populated when a hash function is registered by the Register function. It's effectively a shortcut for asking Size() on the hash.Hash.

View Source
var ErrLenTooLarge = errors.New("requested length was too large for digest")

ErrLenTooLarge is returned when the hash function cannot produce the requested number of bytes

View Source
var ErrSumNotSupported = errors.New("no such hash registered")

ErrSumNotSupported is returned when the Sum function code is not implemented

Functions

func GetHasher

func GetHasher(indicator uint64) (hash.Hash, error)

GetHasher returns a new hash.Hash according to the indicator code number provided.

The indicator code should be per the numbers reserved and described in https://github.com/multiformats/multicodec/blob/master/table.csv .

The actual hashers available are determined by what has been registered. The registry automatically contains those hashers which are available in the golang standard libraries (which includes md5, sha1, sha256, sha384, sha512, and the "identity" mulithash, among others). Other hash implementations can be made available by using the Register function. The 'go-mulithash/register/*' packages can also be imported to gain more common hash functions.

If an error is returned, it will match `errors.Is(err, ErrSumNotSupported)`.

func GetVariableHasher added in v0.2.1

func GetVariableHasher(indicator uint64, sizeHint int) (hash.Hash, error)

GetVariableHasher returns a new hash.Hash according to the indicator code number provided, with the specified size hint.

NOTE: The size hint is only a hint. Hashers will attempt to produce at least the number of requested bytes, but may not.

This function can fail if either the hash code is not registered, or the passed size hint is statically incompatible with the specified hash function.

func Register

func Register(indicator uint64, hasherFactory func() hash.Hash)

Register adds a new hash to the set available from GetHasher and Sum.

Register has a global effect and should only be used at package init time to avoid data races.

The indicator code should be per the numbers reserved and described in https://github.com/multiformats/multicodec/blob/master/table.csv .

If Register is called with the same indicator code more than once, the last call wins. In practice, this means that if an application has a strong opinion about what implementation to use for a certain hash (e.g., perhaps they want to override the sha256 implementation to use a special hand-rolled assembly variant rather than the stdlib one which is registered by default), then this can be done by making a Register call with that effect at init time in the application's main package. This should have the desired effect because the root of the import tree has its init time effect last.

func RegisterVariableSize added in v0.2.1

func RegisterVariableSize(indicator uint64, hasherFactory func(sizeHint int) (hash.Hash, bool))

RegisterVariableSize is like Register, but adds a new variable-sized hasher factory that takes a size hint.

When passed -1, the hasher should produce digests with the hash-function's default length. When passed a non-negative integer, the hasher should try to produce digests of at least the specified size.

Types

This section is empty.

Jump to

Keyboard shortcuts

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