chainhash

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: Unlicense, ISC Imports: 7 Imported by: 0

README

chainhash

ISC License

chainhash provides a generic hash type and associated functions that allows the specific hash algorithm to be abstracted.

Installation and Updating

$ go get -u mleku.online/git/ec/chainhash

License

Package chainhash is licensed under the copyfree ISC License.

Documentation

Overview

Package chainhash provides abstracted hash functionality.

This package provides a generic hash type and associated functions that allows the specific hash algorithm to be abstracted.

Index

Constants

View Source
const (
	// HashSize of array used to store hashes.  See Hash.
	HashSize = 32
	// MaxHashStringSize is the maximum length of a Hash hash string.
	MaxHashStringSize = HashSize * 2
)

Variables

View Source
var (
	// TagBIP0340Challenge is the BIP-0340 tag for challenges.
	TagBIP0340Challenge = by("BIP0340/challenge")
	// TagBIP0340Aux is the BIP-0340 tag for aux data.
	TagBIP0340Aux = by("BIP0340/aux")
	// TagBIP0340Nonce is the BIP-0340 tag for nonces.
	TagBIP0340Nonce = by("BIP0340/nonce")
	// TagTapSighash is the tag used by BIP 341 to generate the sighash
	// flags.
	TagTapSighash = by("TapSighash")
	// TagTapLeaf is the message tag prefix used to compute the hash
	// digest of a tapscript leaf.
	TagTapLeaf = by("TapLeaf")
	// TagTapBranch is the message tag prefix used to compute the
	// hash digest of two tap leaves into a taproot branch node.
	TagTapBranch = by("TapBranch")
	// TagTapTweak is the message tag prefix used to compute the hash tweak
	// used to enable a public key to commit to the taproot branch root
	// for the witness program.
	TagTapTweak = by("TapTweak")
)
View Source
var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes",
	MaxHashStringSize)

ErrHashStrSize describes an error that indicates the caller specified a hash string that has too many characters.

Functions

func Decode

func Decode(dst *Hash, src string) er

Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.

func DoubleHashB

func DoubleHashB(b by) by

DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.

func HashB

func HashB(b by) by

HashB calculates hash(b) and returns the resulting bytes.

Types

type Hash

type Hash [HashSize]byte

Hash is used in several of the bitcoin messages and common structures. It typically represents the double sha256 of data.

func DoubleHashH

func DoubleHashH(b by) Hash

DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a Hash.

func HashH

func HashH(b by) Hash

HashH calculates hash(b) and returns the resulting bytes as a Hash.

func NewHash

func NewHash(newHash by) (*Hash, er)

NewHash returns a new Hash from a byte slice. An error is returned if the number of bytes passed in is not HashSize.

func NewHashFromStr

func NewHashFromStr(hash string) (*Hash, er)

NewHashFromStr creates a Hash from a hash string. The string should be the hexadecimal string of a byte-reversed hash, but any missing characters result in zero padding at the end of the Hash.

func TaggedHash

func TaggedHash(tag by, msgs ...by) *Hash

TaggedHash implements the tagged hash scheme described in BIP-340. We use sha-256 to bind a message hash to a specific context using a tag: sha256(sha256(tag) || sha256(tag) || msg).

func (*Hash) CloneBytes

func (hash *Hash) CloneBytes() by

CloneBytes returns a copy of the bytes which represent the hash as a byte slice.

NOTE: It is generally cheaper to just slice the hash directly thereby reusing the same bytes rather than calling this method.

func (*Hash) IsEqual

func (hash *Hash) IsEqual(target *Hash) bo

IsEqual returns true if target is the same as hash.

func (Hash) MarshalJSON

func (hash Hash) MarshalJSON() (by, er)

MarshalJSON serialises the hash as a JSON appropriate string value.

func (*Hash) SetBytes

func (hash *Hash) SetBytes(newHash by) er

SetBytes sets the bytes which represent the hash. An error is returned if the number of bytes passed in is not HashSize.

func (Hash) String

func (hash Hash) String() string

String returns the Hash as the hexadecimal string of the byte-reversed hash.

func (*Hash) UnmarshalJSON

func (hash *Hash) UnmarshalJSON(input by) er

UnmarshalJSON parses the hash with JSON appropriate string value.

Jump to

Keyboard shortcuts

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