Golang crypto/ecdsa.GenerateKey() function example

package crypto/ecdsa

GenerateKey generates a public and private key pair.

Golang crypto/ecdsa.GenerateKey() function usage example

 pubkeyCurve := elliptic.P256() //see http://golang.org/pkg/crypto/elliptic/#P256
 privatekey := new(ecdsa.PrivateKey)
 privatekey, err := ecdsa.GenerateKey(pubkeyCurve, rand.Reader) // this generates a public & private key pair

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 var pubkey ecdsa.PublicKey
 pubkey = privatekey.PublicKey

 fmt.Println("Private Key :")
 fmt.Printf("%x \n", privatekey)

 fmt.Println("Public Key :")
 fmt.Printf("%x \n",pubkey)

See ECDSA tutorial for full example

Reference :

http://golang.org/pkg/crypto/ecdsa/#GenerateKey

Advertisement