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
- Variables
- func Decode(dst *Hash, src string) error
- func DoubleHashB(b []byte) []byte
- func FNVHash32B(b []byte) []byte
- func FNVHash32uint(b []byte) uint32
- func HashB(b []byte) []byte
- func THashB(b []byte) []byte
- type Hash
- func (h Hash) AsBytes() []byte
- func (h *Hash) CloneBytes() []byte
- func (h *Hash) Difficulty() (difficulty int)
- func (h *Hash) IsEqual(target *Hash) bool
- func (h *Hash) MarshalHash() (o []byte, err error)
- func (h Hash) MarshalJSON() ([]byte, error)
- func (h Hash) MarshalYAML() (interface{}, error)
- func (h *Hash) Msgsize() (s int)
- func (h *Hash) SetBytes(newHash []byte) error
- func (h Hash) Short(n int) string
- func (h Hash) String() string
- func (h *Hash) UnmarshalJSON(data []byte) (err error)
- func (h *Hash) UnmarshalYAML(unmarshal func(interface{}) error) error
- type HashSuite
Constants ¶
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.
Variables ¶
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 ¶
Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.
func DoubleHashB ¶
DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
func FNVHash32B ¶
FNVHash32B calculates hash(b) into [0, 2^64] and returns the resulting bytes.
func FNVHash32uint ¶
FNVHash32uint return the uint32 value of fnv hash 32 of b.
Types ¶
type Hash ¶
Hash is used in several of the bitcoin messages and common structures. It typically represents the double sha256 of data.
func DoubleHashH ¶
DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a Hash.
func NewHash ¶
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 ¶
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 ¶
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 ¶
Difficulty returns the leading Zero **bit** count of Hash in binary.
return -1 indicate the Hash pointer is nil.
func (*Hash) MarshalHash ¶
MarshalHash marshals for hash.
func (Hash) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Hash) MarshalYAML ¶
MarshalYAML implements the yaml.Marshaler interface.
func (*Hash) Msgsize ¶
Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message.
func (*Hash) SetBytes ¶
SetBytes sets the bytes which represent the hash. An error is returned if the number of bytes passed in is not HashSize.
func (*Hash) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Hash) UnmarshalYAML ¶
UnmarshalYAML implements the yaml.Unmarshaler interface.