Golang : Example for DSA(Digital Signature Algorithm) package functions
There are not many examples or tutorials out there on how to fully utilize the crypto/dsa
package. In this tutorial, we will learn how to utilize the crypto/dsa
functions to digitally sign our message and verify the signature.
This source code below will :
- 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
dsaexample.go
package main
import (
"crypto/rand"
"crypto/dsa"
"crypto/md5"
"hash"
"fmt"
"os"
"io"
"math/big"
)
func main() {
params := new(dsa.Parameters)
// see http://golang.org/pkg/crypto/dsa/#ParameterSizes
if err := dsa.GenerateParameters(params, rand.Reader, dsa.L1024N160); err != nil {
fmt.Println(err)
os.Exit(1)
}
privatekey := new(dsa.PrivateKey)
privatekey.PublicKey.Parameters = *params
dsa.GenerateKey(privatekey, rand.Reader) // this generates a public & private key pair
var pubkey dsa.PublicKey
pubkey = privatekey.PublicKey
fmt.Println("Private Key :")
fmt.Printf("%x \n", privatekey)
fmt.Println("Public Key :")
fmt.Printf("%x \n",pubkey)
// Sign
var h hash.Hash
h = md5.New()
r := big.NewInt(0)
s := big.NewInt(0)
io.WriteString(h, "This is the message to be signed and verified!")
signhash := h.Sum(nil)
r, s, err := dsa.Sign(rand.Reader, privatekey, signhash)
if err != nil {
fmt.Println(err)
}
signature := r.Bytes()
signature = append(signature, s.Bytes()...)
fmt.Printf("Signature : %x\n", signature)
// Verify
verifystatus := dsa.Verify(&pubkey, signhash, r, s)
fmt.Println(verifystatus) // should be true
// we add additional data to change the signhash
io.WriteString(h, "This message is NOT to be signed and verified!")
signhash = h.Sum(nil)
verifystatus = dsa.Verify(&pubkey, signhash, r, s)
fmt.Println(verifystatus) // should be false
}
An example output(note : public, private keys and signature values will be different each time the code is executed) :
Private Key :
{{{de735666a2220833b0b07f88c5ff30434e4af53f5e53c0e397057902a.....}
Public Key : {{de735666a2220833b0b07f88c5ff30434e4af53f5e53c0e397057902a......}
Signature :
75483cc98f4587b9ab4e8336b873e8eddd2fb41b594db267ce4bc09285ec15d63d17f6ec82989cd3
true
false
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
+27.5k Golang : Saving(serializing) and reading file with GOB
+10.4k SSL : The certificate is not trusted because no issuer chain was provided
+14k Golang : Get timezone offset from date or timestamp
+6.3k Golang : Calculate how many weeks left to go in a given year
+4.3k Golang : How to deal with configuration data?
+12.5k Golang : Check if an integer is negative or positive
+5.6k Golang : How to setup a disk space used monitoring service with Telegram bot
+6.1k Web : How to see your website from different countries?
+4.8k Unix/Linux : Get reboot history or check when was the last reboot date
+10.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+4.1k Golang : Check if a word is countable or not
+5.7k Golang : Muxing with Martini example