github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/crypto.go (about)

     1  // Copyright (C) 2018 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 nvm
    20  
    21  import "C"
    22  
    23  import (
    24  	"crypto/md5"
    25  
    26  	"github.com/nebulasio/go-nebulas/core"
    27  	"github.com/nebulasio/go-nebulas/crypto/hash"
    28  	"github.com/nebulasio/go-nebulas/crypto/keystore"
    29  	"github.com/nebulasio/go-nebulas/util/byteutils"
    30  	"github.com/nebulasio/go-nebulas/util/logging"
    31  	"github.com/sirupsen/logrus"
    32  )
    33  
    34  // Sha256Func ..
    35  //export Sha256Func
    36  func Sha256Func(data *C.char, gasCnt *C.size_t) *C.char {
    37  	s := C.GoString(data)
    38  	*gasCnt = C.size_t(len(s) + CryptoSha256GasBase)
    39  
    40  	r := hash.Sha256([]byte(s))
    41  	return C.CString(byteutils.Hex(r))
    42  }
    43  
    44  // Sha3256Func ..
    45  //export Sha3256Func
    46  func Sha3256Func(data *C.char, gasCnt *C.size_t) *C.char {
    47  	s := C.GoString(data)
    48  	*gasCnt = C.size_t(len(s) + CryptoSha3256GasBase)
    49  
    50  	r := hash.Sha3256([]byte(s))
    51  	return C.CString(byteutils.Hex(r))
    52  }
    53  
    54  // Ripemd160Func ..
    55  //export Ripemd160Func
    56  func Ripemd160Func(data *C.char, gasCnt *C.size_t) *C.char {
    57  	s := C.GoString(data)
    58  	*gasCnt = C.size_t(len(s) + CryptoRipemd160GasBase)
    59  
    60  	r := hash.Ripemd160([]byte(s))
    61  	return C.CString(byteutils.Hex(r))
    62  }
    63  
    64  // RecoverAddressFunc ..
    65  //export RecoverAddressFunc
    66  func RecoverAddressFunc(alg int, data, sign *C.char, gasCnt *C.size_t) *C.char {
    67  	d := C.GoString(data)
    68  	s := C.GoString(sign)
    69  
    70  	*gasCnt = C.size_t(CryptoRecoverAddressGasBase)
    71  
    72  	plain, err := byteutils.FromHex(d)
    73  	if err != nil {
    74  		logging.VLog().WithFields(logrus.Fields{
    75  			"hash": d,
    76  			"sign": s,
    77  			"alg":  alg,
    78  			"err":  err,
    79  		}).Debug("convert hash to byte array error.")
    80  		return nil
    81  	}
    82  	cipher, err := byteutils.FromHex(s)
    83  	if err != nil {
    84  		logging.VLog().WithFields(logrus.Fields{
    85  			"data": d,
    86  			"sign": s,
    87  			"alg":  alg,
    88  			"err":  err,
    89  		}).Debug("convert sign to byte array error.")
    90  		return nil
    91  	}
    92  	addr, err := core.RecoverSignerFromSignature(keystore.Algorithm(alg), plain, cipher)
    93  	if err != nil {
    94  		logging.VLog().WithFields(logrus.Fields{
    95  			"data": d,
    96  			"sign": s,
    97  			"alg":  alg,
    98  			"err":  err,
    99  		}).Debug("recover address error.")
   100  		return nil
   101  	}
   102  
   103  	return C.CString(addr.String())
   104  }
   105  
   106  // Md5Func ..
   107  //export Md5Func
   108  func Md5Func(data *C.char, gasCnt *C.size_t) *C.char {
   109  	s := C.GoString(data)
   110  	*gasCnt = C.size_t(len(s) + CryptoMd5GasBase)
   111  
   112  	r := md5.Sum([]byte(s))
   113  	return C.CString(byteutils.Hex(r[:]))
   114  }
   115  
   116  // Base64Func ..
   117  //export Base64Func
   118  func Base64Func(data *C.char, gasCnt *C.size_t) *C.char {
   119  	s := C.GoString(data)
   120  	*gasCnt = C.size_t(len(s) + CryptoBase64GasBase)
   121  
   122  	r := hash.Base64Encode([]byte(s))
   123  	return C.CString(string(r))
   124  }