chainhash

package
v2.4.6 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: ISC Imports: 9 Imported by: 5

Documentation

Overview

Package chainhash provides a type for representing hashes used in the Bitcoin protocol and provides functions for working with them.

Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2015 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const HashSize = 32

HashSize of an array used to store hashes. See Hash.

View Source
const MaxHashStringSize = HashSize * 2

MaxHashStringSize is the maximum length of a Hash string.

Variables

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

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 []byte) Hash

DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a 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 (h *Hash) CloneBytes() []byte

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

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

func (Hash) Equal

func (h Hash) Equal(other Hash) bool

Equal compares two Hashes for equality.

func (*Hash) IsEqual

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

IsEqual returns true if target is the same as hash.

func (*Hash) Marshal

func (h *Hash) Marshal() ([]byte, error)

Marshal converts a chainhash.Hash to a protobuf []byte.

func (*Hash) MarshalJSON

func (h *Hash) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the hash as a hexadecimal

func (*Hash) ProtoReflect

func (h *Hash) ProtoReflect() protoreflect.Message

ProtoReflect implements proto.Message

func (*Hash) Scan

func (h *Hash) Scan(value interface{}) error

Scan implements the sql.Scanner

func (*Hash) SetBytes

func (h *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) Size

func (h *Hash) Size() int

Size implements proto.Sizer

func (Hash) String

func (h Hash) String() string

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

func (*Hash) Unmarshal

func (h *Hash) Unmarshal(data []byte) error

Unmarshal converts a protobuf []byte to chainhash.Hash.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded hash string and sets the hash to the

func (*Hash) Value

func (h *Hash) Value() (driver.Value, error)

Value implements the driver.Valuer

Jump to

Keyboard shortcuts

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