hash

package
v0.0.0-...-020e20f Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: Apache-2.0 Imports: 10 Imported by: 1

README

Hash Lib ported from btcd

GPG Verification Key

All official release tags are signed by Conformal so users can ensure the code has not been tampered with and is coming from the btcsuite developers. To verify the signature perform the following:

  • Download the public key from the Conformal website at https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt

  • Import the public key into your GPG keyring:

    gpg --import GIT-GPG-KEY-conformal.txt
    
  • Verify the release tag with the following command where TAG_NAME is a placeholder for the specific tag:

    git tag -v TAG_NAME
    

License

Package hash is licensed under the MIT License.

Package chainhash is licensed under the copyfree ISC License.

Documentation

Overview

Package hash provides abstracted hash functionality.

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

Q: WHY SHA-256 twice?

A: SHA-256(SHA-256(x)) was proposed by Ferguson and Schneier in their excellent book "Practical Cryptography" (later updated by Ferguson, Schneier, and Kohno and renamed "Cryptography Engineering") as a way to make SHA-256 invulnerable to "length-extension" attack. They called it "SHA-256d". We started using SHA-256d for everything when we launched the Tahoe-LAFS project in 2006, on the principle that it is hardly less efficient than SHA-256, and that it frees us from having to reason about whether length-extension attacks are dangerous every place that we use a hash function. I wouldn't be surprised if the inventors of Bitcoin used it for similar reasons. Why not use SHA-256d instead of SHA-256?

Note that the SHA-3 project required all candidates to have some method of preventing length-extension attacks. Some of them use a method that is rather like SHA-256d, i.e. they do an extra "finalization" hash of their state at the end, before emitting a result.

--From: https://crypto.stackexchange.com/a/884.

Index

Constants

View Source
const HashBSize = sha256.Size

HashBSize is the size of HashB.

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.

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 FNVHash32B

func FNVHash32B(b []byte) []byte

FNVHash32B calculates hash(b) into [0, 2^64] and returns the resulting bytes.

func FNVHash32uint

func FNVHash32uint(b []byte) uint32

FNVHash32uint return the uint32 value of fnv hash 32 of b.

func HashB

func HashB(b []byte) []byte

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

func THashB

func THashB(b []byte) []byte

THashB is a combination of blake2b-512 and SHA256

The cryptographic hash function BLAKE2 is an improved version of the

SHA-3 finalist BLAKE.

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 THashH

func THashH(b []byte) Hash

THashH calculates sha256(blake2b-512(b)) and returns the resulting bytes as a Hash.

func (Hash) AsBytes

func (h Hash) AsBytes() []byte

AsBytes returns internal bytes of 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 cheaper to just slice the hash directly thereby reusing the same bytes rather than calling this method.

func (*Hash) Difficulty

func (h *Hash) Difficulty() (difficulty int)

Difficulty returns the leading Zero **bit** count of Hash in binary.

return -1 indicate the Hash pointer is nil.

func (*Hash) IsEqual

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

IsEqual returns true if target is the same as hash.

func (*Hash) MarshalHash

func (h *Hash) MarshalHash() (o []byte, err error)

MarshalHash marshals for hash.

func (Hash) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (Hash) MarshalYAML

func (h Hash) MarshalYAML() (interface{}, error)

MarshalYAML implements the yaml.Marshaler interface.

func (*Hash) Msgsize

func (h *Hash) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message.

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

func (h Hash) Short(n int) string

Short returns the hexadecimal string of the first `n` reversed byte(s).

func (Hash) String

func (h Hash) String() string

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

func (*Hash) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Hash) UnmarshalYAML

func (h *Hash) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type HashSuite

type HashSuite struct {
	HashLen  int
	HashFunc func([]byte) []byte
}

HashSuite contains the hash length and the func handler.

Jump to

Keyboard shortcuts

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