daghash

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: ISC Imports: 6 Imported by: 0

README

daghash

ISC License GoDoc

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

Documentation

Overview

Package daghash 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 = 32

HashSize of array used to store hashes. See Hash.

View Source
const MaxHashStringSize = HashSize * 2

MaxHashStringSize is the maximum length of a Hash hash string.

View Source
const TxIDSize = HashSize

TxIDSize of array used to store TxID. See TxID.

Variables

View Source
var ErrHashStrSize = errors.Errorf("max hash string length is %d bytes", MaxHashStringSize)

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

Functions

func AreEqual

func AreEqual(first []*Hash, second []*Hash) bool

AreEqual returns true if both slices contain the same hashes. Either slice must not contain duplicates.

func Decode

func Decode(dst *Hash, src string) error

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

func DoubleHashB

func DoubleHashB(b []byte) []byte

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

func HashB

func HashB(b []byte) []byte

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

func HashToBig

func HashToBig(hash *Hash) *big.Int

HashToBig converts a daghash.Hash into a big.Int that can be used to perform math comparisons.

func JoinHashesStrings

func JoinHashesStrings(hashes []*Hash, separator string) string

JoinHashesStrings joins all the stringified hashes separated by a separator

func Less

func Less(a *Hash, b *Hash) bool

Less returns true iff hash a is less than hash b

func LessTxID

func LessTxID(a *TxID, b *TxID) bool

LessTxID returns true if tx ID a is less than tx ID b

func Sort

func Sort(hashes []*Hash)

Sort sorts a slice of hashes

func Strings

func Strings(hashes []*Hash) []string

Strings returns a slice of strings representing the hashes in the given slice of hashes

Types

type Hash

type Hash [HashSize]byte

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

var ZeroHash Hash

ZeroHash is the Hash value of all zero bytes, defined here for convenience.

func DoubleHashH

func DoubleHashH(b []byte) Hash

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

func DoubleHashP

func DoubleHashP(b []byte) *Hash

DoubleHashP calculates hash(hash(b)) and returns the resulting bytes as a pointer to Hash.

func HashH

func HashH(b []byte) Hash

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

func NewHash

func NewHash(newHash []byte) (*Hash, error)

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, error)

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 (*Hash) CloneBytes

func (hash *Hash) CloneBytes() []byte

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

func (hash *Hash) Cmp(target *Hash) int

Cmp compares hash and target and returns:

-1 if hash <  target
 0 if hash == target
+1 if hash >  target

func (*Hash) IsEqual

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

IsEqual returns true if target is the same as hash.

func (*Hash) SetBytes

func (hash *Hash) SetBytes(newHash []byte) error

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.

type TxID

type TxID Hash

TxID is transaction hash not including payload and signature.

var ZeroTxID TxID

ZeroTxID is the Hash value of all zero bytes, defined here for convenience.

func NewTxID

func NewTxID(newTxID []byte) (*TxID, error)

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

func NewTxIDFromStr

func NewTxIDFromStr(idStr string) (*TxID, error)

NewTxIDFromStr creates a TxID 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 (*TxID) CloneBytes

func (txID *TxID) CloneBytes() []byte

CloneBytes returns a copy of the bytes which represent the TxID 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 (*TxID) IsEqual

func (txID *TxID) IsEqual(target *TxID) bool

IsEqual returns true if target is the same as TxID.

func (*TxID) SetBytes

func (txID *TxID) SetBytes(newID []byte) error

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

func (TxID) String

func (txID TxID) String() string

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

Jump to

Keyboard shortcuts

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