Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions

In cryptography, the Elliptic Curve Digital Signature Algorithm (ECDSA) is a variant of the Digital Signature Algorithm (DSA) which uses elliptic curve cryptography. This tutorial is just a slight variant of the previous tutorial for DSA and we will learn how to use the ECDSA functions to do :

  • generate a private key
  • extract the public key from the generated private key
  • use the private key to sign
  • use the public key to verify the signature


 package main

 import (

 func main() {

  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 {

  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)

  // Sign ecdsa style

  var h hash.Hash
  h = md5.New()
  r := big.NewInt(0)
  s := big.NewInt(0)

  io.WriteString(h, "This is a message to be signed and verified by ECDSA!")
  signhash := h.Sum(nil)

  r, s, serr := ecdsa.Sign(rand.Reader, privatekey, signhash)
  if serr != nil {

  signature := r.Bytes()
  signature = append(signature, s.Bytes()...)

  fmt.Printf("Signature : %x\n", signature)

  // Verify
  verifystatus := ecdsa.Verify(&pubkey, signhash, r, s)
  fmt.Println(verifystatus) // should be true

Executing the code will produce the following output( private/public keys will be different for each execution) :

go run ecdsaexample.go

Private Key :

&{{{20821a420} 127e668421cbbf6a80692679560c618d5f06281b02a8323157816e4c7ce50e2b



Public Key :

{{20821a420} 127e668421cbbf6a80692679560c618d5f06281b02a8323157816e4c7ce50e2b


Signature :




By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.