Example of public key signature and verification generated with fabric case

public and private keys generated by Fabric case

The

Fabric case generates public and private key pairs for nodes such as Peer, User, Admin, etc. Public and private keys are shown in the figure below.


example of signing and validating with public and private key pairs

Example code for signing and validating a message with the above public and private key pairs is shown below.

package main

import (
	"crypto/ecdsa"
	"crypto/rand"
	"crypto/sha256"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"io/ioutil"
)

func main() {
	msg := "hello, world"
	hash := sha256.Sum256([]byte(msg))
	msg2 := "Hello, world"
	hash2 := sha256.Sum256([]byte(msg2))

	privBytes, _ := ioutil.ReadFile("./priv_sk")
	blkPriv, _ := pem.Decode(privBytes)
	fmt.Println("priv_sk  type:", blkPriv.Type)
	key, _ := x509.ParsePKCS8PrivateKey(blkPriv.Bytes)
	ecdsaKey := key.(*ecdsa.PrivateKey)
	r, s, _ := ecdsa.Sign(rand.Reader, ecdsaKey, hash[:])

	certBytes, _ := ioutil.ReadFile("./cert.pem")
	blkCert, _ := pem.Decode(certBytes)
	fmt.Println("cert.pem type:", blkCert.Type)
	cert, _ := x509.ParseCertificate(blkCert.Bytes)
	pubkey := cert.PublicKey.(*ecdsa.PublicKey)
	ok := ecdsa.Verify(pubkey, hash[:], r, s)
	fmt.Println("verify hash(shoule be true):", ok)

	ok = ecdsa.Verify(pubkey, hash2[:], r, s)
	fmt.Println("verify hash2(shoule be false):", ok)
}

compiles and runs as shown below.

Read More: