CovenantSQL: Index | Files

package hash

import ""

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.



Package Files

doc.go hash.go hashfuncs.go


const HashBSize = sha256.Size

HashBSize is the size of HashB.

const HashSize = 32

HashSize of array used to store hashes. See Hash.

const MaxHashStringSize = HashSize * 2

MaxHashStringSize is the maximum length of a Hash hash string.


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.

func Decode Uses

func Decode(dst *Hash, src string) error

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

func DoubleHashB Uses

func DoubleHashB(b []byte) []byte

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

func FNVHash32B Uses

func FNVHash32B(b []byte) []byte

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

func FNVHash32uint Uses

func FNVHash32uint(b []byte) uint32

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

func HashB Uses

func HashB(b []byte) []byte

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

func THashB Uses

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.

type Hash Uses

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 Uses

func DoubleHashH(b []byte) Hash

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

func HashH Uses

func HashH(b []byte) Hash

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

func NewHash Uses

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 Uses

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 Uses

func THashH(b []byte) Hash

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

func (Hash) AsBytes Uses

func (h Hash) AsBytes() []byte

AsBytes returns internal bytes of hash.

func (*Hash) CloneBytes Uses

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 Uses

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 Uses

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

IsEqual returns true if target is the same as hash.

func (*Hash) MarshalHash Uses

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

MarshalHash marshals for hash.

func (Hash) MarshalJSON Uses

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

MarshalJSON implements the json.Marshaler interface.

func (Hash) MarshalYAML Uses

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

MarshalYAML implements the yaml.Marshaler interface.

func (*Hash) Msgsize Uses

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 Uses

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 Uses

func (h Hash) Short(n int) string

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

func (Hash) String Uses

func (h Hash) String() string

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

func (*Hash) UnmarshalJSON Uses

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Hash) UnmarshalYAML Uses

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

UnmarshalYAML implements the yaml.Unmarshaler interface.

type HashSuite Uses

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

HashSuite contains the hash length and the func handler.

Package hash imports 10 packages (graph) and is imported by 38 packages. Updated 2019-04-05. Refresh now. Tools for package owners.