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
ecdsaexample.go
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"fmt"
"hash"
"io"
"math/big"
"os"
)
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 {
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)
// 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 {
fmt.Println(err)
os.Exit(1)
}
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
3b776e2f6d9febc03d3abdf91c16d15396dc6f72bec3e259df2bfdec8fe41f89}
44d0f3819c4153a23f42263034d450c3038a305038285a04f4e068b56ebe5393}
Public Key :
{{20821a420} 127e668421cbbf6a80692679560c618d5f06281b02a8323157816e4c7ce50e2b
3b776e2f6d9febc03d3abdf91c16d15396dc6f72bec3e259df2bfdec8fe41f89}
Signature :
d7b0dd08b4de09da1c70567655ba16a5437a75bdaa4917ab509bf663e71d1aeef0d6cc
11e458e66e15a4ffae70ab434eb586514e22d95e89f75ec96c48bcb4f5
true
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.
Advertisement
Tutorials
+26k Golang : Record voice(audio) from microphone to .WAV file
+9.5k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+3.7k Linux : sudo yum updates not working
+24.6k Mac/Linux and Golang : Fix bind: address already in use error
+22.1k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+6.7k Golang : Test if an input is an Armstrong number example
+14.8k Golang : Delete files by extension
+11.1k Golang : Display list of countries and ISO codes
+5.1k Golang : Extract XML attribute data with attr field tag example
+16.9k Golang : Get download file size
+27.9k Golang : missing Git command
+10.3k Golang : Secure file deletion with wipe example