func (h Hash) MarshalYAML() (interface{}, error) {
	return h.String(), nil
}
MarshalYAML implements the yaml.Marshaler interface.
package hash
import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"math/bits"
	hsp "github.com/SQLess/HashStablePack/marshalhash"
	"github.com/SQLess/SQLess/utils/log"
)
const HashSize = 32
const MaxHashStringSize = HashSize * 2
var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)
type Hash [HashSize]byte
func (h Hash) String() string {
	for i := 0; i < HashSize/2; i++ {
		h[i], h[HashSize-1-i] = h[HashSize-1-i], h[i]
	}
	return hex.EncodeToString(h[:])
}
func (h Hash) Short(n int) string {
	for i := 0; i < HashSize/2; i++ {
		h[i], h[HashSize-1-i] = h[HashSize-1-i], h[i]
	}
	var l = HashSize
	if n < l {
		l = n
	}
	return hex.EncodeToString(h[:l])
}
func (h Hash) AsBytes() []byte {
	return h[:]
}
func (h *Hash) CloneBytes() []byte {
	newHash := make([]byte, HashSize)
	copy(newHash, h[:])
	return newHash
}
func (h *Hash) MarshalHash() (o []byte, err error) {
	return h.CloneBytes(), nil
}
func (h *Hash) Msgsize() (s int) {
	return hsp.BytesPrefixSize + HashSize
}
func (h *Hash) SetBytes(newHash []byte) error {
	nhlen := len(newHash)
	if nhlen != HashSize {
		return fmt.Errorf("invalid hash length of %v, want %v", nhlen,
			HashSize)
	}
	copy(h[:], newHash)
	return nil
}
func (h *Hash) IsEqual(target *Hash) bool {
	if h == nil && target == nil {
		return true
	}
	if h == nil || target == nil {
		return false
	}
	return *h == *target
}
func (h *Hash) Difficulty() (difficulty int) {
	if h == nil {
		return -1
	}
	for i := range *h {
		v := (*h)[HashSize-i-1]
		if v != byte(0) {
			difficulty = 8 * i
			difficulty += bits.LeadingZeros8(v)
			return
		}
	}
	return HashSize * 8
}
func (h Hash) MarshalJSON() ([]byte, error) {
	return json.Marshal(h.String())
}
func (h *Hash) UnmarshalJSON(data []byte) (err error) {
	var s string
	if err = json.Unmarshal(data, &s); err != nil {
		return
	}
	if err = Decode(h, s); err != nil {
		return
	}
	return nil
}