github.com/annchain/OG@v0.0.9/og_interface/hash32.go (about)

     1  package og_interface
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"fmt"
     6  	"github.com/annchain/commongo/hexutil"
     7  	"github.com/annchain/commongo/marshaller"
     8  	"github.com/annchain/commongo/math"
     9  	"github.com/annchain/commongo/utilfuncs"
    10  	"math/big"
    11  	"math/rand"
    12  )
    13  
    14  const (
    15  	Hash32Length = 32
    16  )
    17  
    18  type Hash32 [Hash32Length]byte
    19  
    20  func (a *Hash32) HashKey() HashKey {
    21  	return HashKey(a[:])
    22  }
    23  
    24  func (a *Hash32) HashShortString() string {
    25  	return hexutil.ToHex(a[:10])
    26  }
    27  
    28  func (a *Hash32) HashString() string {
    29  	return hexutil.ToHex(a.Bytes())
    30  }
    31  
    32  func (a *Hash32) Bytes() []byte {
    33  	return a[:]
    34  }
    35  
    36  func (a *Hash32) Hex() string {
    37  	return hexutil.ToHex(a[:])
    38  }
    39  
    40  func (a *Hash32) Length() int {
    41  	return Hash32Length
    42  }
    43  
    44  func (a *Hash32) FromBytes(b []byte) {
    45  	copy(a[:], b)
    46  }
    47  
    48  func (a *Hash32) FromHex(s string) (err error) {
    49  	bytes, err := hexutil.FromHex(s)
    50  	if err != nil {
    51  		return
    52  	}
    53  	a.FromBytes(bytes)
    54  	return
    55  }
    56  
    57  func (a *Hash32) FromHexNoError(s string) {
    58  	err := a.FromHex(s)
    59  	utilfuncs.PanicIfError(err, "HexToHash32")
    60  }
    61  
    62  func (a *Hash32) Cmp(another FixLengthBytes) int {
    63  	return BytesCmp(a, another)
    64  }
    65  
    66  // marshalling part
    67  
    68  func (a *Hash32) MsgSize() int {
    69  	return 1 + Hash32Length
    70  }
    71  
    72  func (a *Hash32) MarshalMsg() ([]byte, error) {
    73  	data := marshaller.InitIMarshallerBytes(a.MsgSize())
    74  	data, pos := marshaller.EncodeHeader(data, 0, a.MsgSize())
    75  
    76  	// add lead
    77  	data[pos] = FlagHash32
    78  	// add hash data
    79  	copy(data[pos+1:], a[:])
    80  	return data, nil
    81  }
    82  
    83  func (a *Hash32) UnmarshalMsg(data []byte) ([]byte, error) {
    84  	data, msgLen, err := marshaller.DecodeHeader(data)
    85  	if err != nil {
    86  		return data, fmt.Errorf("get marshaller header error: %v", err)
    87  	}
    88  
    89  	msgSize := a.MsgSize()
    90  	if len(data) < msgSize || msgLen != msgSize {
    91  		return data, fmt.Errorf("bytes not enough for hash32, should be: %d, get: %d, msgLen: %d", msgSize, len(data), msgLen)
    92  	}
    93  	lead := data[0]
    94  	if lead != FlagHash32 {
    95  		return data, fmt.Errorf("hash lead error, should be: %x, get: %x", FlagHash32, lead)
    96  	}
    97  	data = data[1:]
    98  
    99  	copy(a[:], data[:Hash32Length])
   100  	return data[Hash32Length:], nil
   101  }
   102  
   103  func BytesToHash32(b []byte) *Hash32 {
   104  	h := &Hash32{}
   105  	h.FromBytes(b)
   106  	return h
   107  }
   108  
   109  func HexToHash32(hex string) (*Hash32, error) {
   110  	h := &Hash32{}
   111  	err := h.FromHex(hex)
   112  	return h, err
   113  }
   114  
   115  func BigToHash32(v *big.Int) *Hash32 {
   116  	h := &Hash32{}
   117  	h.FromBytes(v.Bytes())
   118  	return h
   119  }
   120  
   121  func RandomHash32() *Hash32 {
   122  	v := math.NewBigInt(rand.Int63())
   123  	sh := BigToHash32(v.Value)
   124  	h := sha256.New()
   125  	data := []byte("456544546fhjsiodiruheswer8ih")
   126  	h.Write(sh[:])
   127  	h.Write(data)
   128  	sum := h.Sum(nil)
   129  	sh.FromBytes(sum)
   130  	return sh
   131  }