github.com/igggame/nebulas-go@v2.1.0+incompatible/util/byteutils/bytes.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package byteutils
    20  
    21  import (
    22  	"bytes"
    23  	"encoding/binary"
    24  	"encoding/hex"
    25  	"hash/fnv"
    26  
    27  	"github.com/btcsuite/btcutil/base58"
    28  )
    29  
    30  // Hash by Sha3-256
    31  type Hash []byte
    32  
    33  // HexHash is the hex string of a hash
    34  type HexHash string
    35  
    36  // Hex return hex encoded hash.
    37  func (h Hash) Hex() HexHash {
    38  	return HexHash(Hex(h))
    39  }
    40  
    41  // Base58 return base58 encodes string
    42  func (h Hash) Base58() string {
    43  	return base58.Encode(h)
    44  }
    45  
    46  // Equals compare two Hash. True is equal, otherwise false.
    47  func (h Hash) Equals(b Hash) bool {
    48  	return bytes.Compare(h, b) == 0
    49  }
    50  
    51  func (h Hash) String() string {
    52  	return string(h.Hex())
    53  }
    54  
    55  // Hash return hex decoded hash.
    56  func (hh HexHash) Hash() (Hash, error) {
    57  	v, err := FromHex(string(hh))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return Hash(v), nil
    62  }
    63  
    64  // Encode encodes object to Encoder.
    65  func Encode(s interface{}, enc Encoder) ([]byte, error) {
    66  	return enc.EncodeToBytes(s)
    67  }
    68  
    69  // Decode decodes []byte from Decoder.
    70  func Decode(data []byte, dec Decoder) (interface{}, error) {
    71  	return dec.DecodeFromBytes(data)
    72  }
    73  
    74  // Hex encodes []byte to Hex.
    75  func Hex(data []byte) string {
    76  	return hex.EncodeToString(data)
    77  }
    78  
    79  // FromHex decodes string from Hex.
    80  func FromHex(data string) ([]byte, error) {
    81  	return hex.DecodeString(data)
    82  }
    83  
    84  // Uint64 encodes []byte.
    85  func Uint64(data []byte) uint64 {
    86  	return binary.BigEndian.Uint64(data)
    87  }
    88  
    89  // FromUint64 decodes unit64 value.
    90  func FromUint64(v uint64) []byte {
    91  	b := make([]byte, 8)
    92  	binary.BigEndian.PutUint64(b, v)
    93  	return b
    94  }
    95  
    96  // Uint32 encodes []byte.
    97  func Uint32(data []byte) uint32 {
    98  	return binary.BigEndian.Uint32(data)
    99  }
   100  
   101  // FromUint32 decodes uint32.
   102  func FromUint32(v uint32) []byte {
   103  	b := make([]byte, 4)
   104  	binary.BigEndian.PutUint32(b, v)
   105  	return b
   106  }
   107  
   108  // Uint16 encodes []byte.
   109  func Uint16(data []byte) uint16 {
   110  	return binary.BigEndian.Uint16(data)
   111  }
   112  
   113  // FromUint16 decodes uint16.
   114  func FromUint16(v uint16) []byte {
   115  	b := make([]byte, 2)
   116  	binary.BigEndian.PutUint16(b, v)
   117  	return b
   118  }
   119  
   120  // Int64 encodes []byte.
   121  func Int64(data []byte) int64 {
   122  	return int64(binary.BigEndian.Uint64(data))
   123  }
   124  
   125  // FromInt64 decodes int64 v.
   126  func FromInt64(v int64) []byte {
   127  	b := make([]byte, 8)
   128  	binary.BigEndian.PutUint64(b, uint64(v))
   129  	return b
   130  }
   131  
   132  // Int32 encodes []byte.
   133  func Int32(data []byte) int32 {
   134  	return int32(binary.BigEndian.Uint32(data))
   135  }
   136  
   137  // FromInt32 decodes int32 v.
   138  func FromInt32(v int32) []byte {
   139  	b := make([]byte, 4)
   140  	binary.BigEndian.PutUint32(b, uint32(v))
   141  	return b
   142  }
   143  
   144  // Int16 encode []byte.
   145  func Int16(data []byte) int16 {
   146  	return int16(binary.BigEndian.Uint16(data))
   147  }
   148  
   149  // FromInt16 decodes int16 v.
   150  func FromInt16(v int16) []byte {
   151  	b := make([]byte, 2)
   152  	binary.BigEndian.PutUint16(b, uint16(v))
   153  	return b
   154  }
   155  
   156  // Equal checks whether byte slice a and b are equal.
   157  func Equal(a []byte, b []byte) bool {
   158  	if len(a) != len(b) {
   159  		return false
   160  	}
   161  	for i := 0; i < len(a); i++ {
   162  		if a[i] != b[i] {
   163  			return false
   164  		}
   165  	}
   166  	return true
   167  }
   168  
   169  // HashBytes return bytes hash
   170  func HashBytes(a []byte) uint32 {
   171  	hasherA := fnv.New32a()
   172  	hasherA.Write(a)
   173  	return hasherA.Sum32()
   174  }
   175  
   176  // Less return if a < b
   177  func Less(a []byte, b []byte) bool {
   178  	return HashBytes(a) < HashBytes(b)
   179  }