github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/secp256k1/subtle/secp256k1_signer_verifier_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package subtle_test 8 9 import ( 10 "crypto/ecdsa" 11 "crypto/rand" 12 "testing" 13 14 "github.com/google/tink/go/subtle/random" 15 "github.com/stretchr/testify/require" 16 17 subtleSignature "github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/secp256k1/subtle" 18 ) 19 20 func TestSignVerify(t *testing.T) { 21 data := random.GetRandomBytes(20) 22 hash := "SHA256" 23 curve := "SECP256K1" 24 encodings := []string{"Bitcoin_DER", "Bitcoin_IEEE_P1363"} 25 26 for _, encoding := range encodings { 27 priv, err := ecdsa.GenerateKey(subtleSignature.GetCurve(curve), rand.Reader) 28 require.NoError(t, err) 29 30 // Use the private key and public key directly to create new instances 31 signer, err := subtleSignature.NewSecp256K1SignerFromPrivateKey(hash, encoding, priv) 32 require.NoError(t, err, "unexpected error when creating Secp256K1Signer") 33 34 verifier, err := subtleSignature.NewSecp256K1VerifierFromPublicKey(hash, encoding, &priv.PublicKey) 35 require.NoError(t, err, "unexpected error when creating ECDSAVerifier") 36 37 signature, err := signer.Sign(data) 38 require.NoError(t, err, "unexpected error when signing") 39 40 err = verifier.Verify(signature, data) 41 require.NoError(t, err, "unexpected error when verifying") 42 43 // Use byte slices to create new instances 44 signer, err = subtleSignature.NewSecp256K1Signer(hash, curve, encoding, priv.D.Bytes()) 45 require.NoError(t, err, "unexpected error when creating Secp256K1Signer") 46 47 verifier, err = subtleSignature.NewSecp256K1Verifier(hash, curve, encoding, priv.X.Bytes(), priv.Y.Bytes()) 48 require.NoError(t, err, "unexpected error when creating ECDSAVerifier") 49 50 signature, err = signer.Sign(data) 51 require.NoError(t, err, "unexpected error when signing") 52 53 err = verifier.Verify(signature, data) 54 require.NoError(t, err, "unexpected error when verifying") 55 } 56 }