github.com/0chain/gosdk@v1.17.11/mobilesdk/sdk/sign.go (about)

     1  package sdk
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/0chain/gosdk/core/encryption"
     7  	"github.com/0chain/gosdk/core/sys"
     8  	"github.com/0chain/gosdk/core/zcncrypto"
     9  	_ "github.com/0chain/gosdk/zboxcore/client" //import it to initialize sys.Sign
    10  )
    11  
    12  var ErrInvalidSignatureScheme = errors.New("invalid_signature_scheme")
    13  
    14  // SignRequest sign data with private key and scheme
    15  //   - privateKey: private key to use for signing
    16  //   - signatureScheme: signature scheme to use for signing
    17  //   - data: data to sign using the private key
    18  func SignRequest(privateKey, signatureScheme string, data string) (string, error) {
    19  	hash := encryption.Hash(data)
    20  	return sys.Sign(hash, signatureScheme, []sys.KeyPair{{
    21  		PrivateKey: privateKey,
    22  	}})
    23  }
    24  
    25  // VerifySignature verify signature with public key, schema and data
    26  //   - publicKey: public key to use for verifying signature
    27  //   - signatureScheme: signature scheme to use for verifying signature
    28  //   - data: data to verify using the public key
    29  //   - signature: signature to verify
    30  func VerifySignature(publicKey, signatureScheme string, data string, signature string) (bool, error) {
    31  
    32  	hash := encryption.Hash(data)
    33  
    34  	signScheme := zcncrypto.NewSignatureScheme(signatureScheme)
    35  	if signScheme != nil {
    36  		err := signScheme.SetPublicKey(publicKey)
    37  		if err != nil {
    38  			return false, err
    39  		}
    40  		return signScheme.Verify(signature, hash)
    41  	}
    42  	return false, ErrInvalidSignatureScheme
    43  }