github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/hash.go (about)

     1  package bc
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"encoding/hex"
     7  	"encoding/json"
     8  	"fmt"
     9  	"io"
    10  
    11  	"golang.org/x/crypto/sha3"
    12  )
    13  
    14  // EmptyStringHash represents a 256-bit hash.
    15  var EmptyStringHash = NewHash(sha3.Sum256(nil))
    16  
    17  // NewHash convert the input byte array to hash
    18  func NewHash(b32 [32]byte) (h Hash) {
    19  	h.V0 = binary.BigEndian.Uint64(b32[0:8])
    20  	h.V1 = binary.BigEndian.Uint64(b32[8:16])
    21  	h.V2 = binary.BigEndian.Uint64(b32[16:24])
    22  	h.V3 = binary.BigEndian.Uint64(b32[24:32])
    23  	return h
    24  }
    25  
    26  // Byte32 return the byte array representation
    27  func (h Hash) Byte32() (b32 [32]byte) {
    28  	binary.BigEndian.PutUint64(b32[0:8], h.V0)
    29  	binary.BigEndian.PutUint64(b32[8:16], h.V1)
    30  	binary.BigEndian.PutUint64(b32[16:24], h.V2)
    31  	binary.BigEndian.PutUint64(b32[24:32], h.V3)
    32  	return b32
    33  }
    34  
    35  // MarshalText satisfies the TextMarshaler interface.
    36  // It returns the bytes of h encoded in hex,
    37  // for formats that can't hold arbitrary binary data.
    38  // It never returns an error.
    39  func (h Hash) MarshalText() ([]byte, error) {
    40  	b := h.Byte32()
    41  	v := make([]byte, 64)
    42  	hex.Encode(v, b[:])
    43  	return v, nil
    44  }
    45  
    46  // UnmarshalText satisfies the TextUnmarshaler interface.
    47  // It decodes hex data from b into h.
    48  func (h *Hash) UnmarshalText(v []byte) error {
    49  	var b [32]byte
    50  	if len(v) != 64 {
    51  		return fmt.Errorf("bad length hash string %d", len(v))
    52  	}
    53  
    54  	_, err := hex.Decode(b[:], v)
    55  	*h = NewHash(b)
    56  	return err
    57  }
    58  
    59  // UnmarshalJSON satisfies the json.Unmarshaler interface.
    60  // If b is a JSON-encoded null, it copies the zero-value into h. Othwerwise, it
    61  // decodes hex data from b into h.
    62  func (h *Hash) UnmarshalJSON(b []byte) error {
    63  	if bytes.Equal(b, []byte("null")) {
    64  		*h = Hash{}
    65  		return nil
    66  	}
    67  
    68  	var s string
    69  	if err := json.Unmarshal(b, &s); err != nil {
    70  		return err
    71  	}
    72  	return h.UnmarshalText([]byte(s))
    73  }
    74  
    75  // Bytes returns the byte representation
    76  func (h Hash) Bytes() []byte {
    77  	b32 := h.Byte32()
    78  	return b32[:]
    79  }
    80  
    81  // WriteTo satisfies the io.WriterTo interface.
    82  func (h Hash) WriteTo(w io.Writer) (int64, error) {
    83  	n, err := w.Write(h.Bytes())
    84  	return int64(n), err
    85  }
    86  
    87  // ReadFrom satisfies the io.ReaderFrom interface.
    88  func (h *Hash) ReadFrom(r io.Reader) (int64, error) {
    89  	var b32 [32]byte
    90  	n, err := io.ReadFull(r, b32[:])
    91  	if err != nil {
    92  		return int64(n), err
    93  	}
    94  
    95  	*h = NewHash(b32)
    96  	return int64(n), nil
    97  }
    98  
    99  // IsZero tells whether a Hash pointer is nil or points to an all-zero
   100  // hash.
   101  func (h *Hash) IsZero() bool {
   102  	return h == nil || *h == Hash{}
   103  }