github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/common.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser 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 ontology 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 Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package common
    20  
    21  import (
    22  	"encoding/hex"
    23  	"math/rand"
    24  	"os"
    25  )
    26  
    27  // GetNonce returns random nonce
    28  func GetNonce() uint64 {
    29  	// Fixme replace with the real random number generator
    30  	nonce := uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
    31  	return nonce
    32  }
    33  
    34  // ToHexString convert []byte to hex string
    35  func ToHexString(data []byte) string {
    36  	return hex.EncodeToString(data)
    37  }
    38  
    39  // HexToBytes convert hex string to []byte
    40  func HexToBytes(value string) ([]byte, error) {
    41  	return hex.DecodeString(value)
    42  }
    43  
    44  func ToArrayReverse(arr []byte) []byte {
    45  	l := len(arr)
    46  	x := make([]byte, 0)
    47  	for i := l - 1; i >= 0; i-- {
    48  		x = append(x, arr[i])
    49  	}
    50  	return x
    51  }
    52  
    53  // FileExisted checks whether filename exists in filesystem
    54  func FileExisted(filename string) bool {
    55  	_, err := os.Stat(filename)
    56  	return err == nil || os.IsExist(err)
    57  }