Golang crypto/elliptic.GenerateKey() function example

package crypto/elliptic

GenerateKey returns a public/private key pair. The private key is generated using the given reader, which must return random data.

Golang crypto/elliptic.GenerateKey() function usage example

 package main

 import (
 "crypto/elliptic"
 "crypto/rand"
 "crypto"
 "fmt"
 "math/big"
 )


 type ellipticPublicKey struct {
 elliptic.Curve
 X, Y *big.Int
 }

 type ellipticPrivateKey struct {
 privk []byte
 }


 func main() {
  var privatekey crypto.PrivateKey
  var publickey crypto.PublicKey
  var err error
  var priv []byte
  var x, y *big.Int


  priv, x, y, err = elliptic.GenerateKey(elliptic.P224(), rand.Reader) //P224 for this example

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

  privatekey = &ellipticPrivateKey {
 privk : priv,
  }

  publickey = &ellipticPublicKey {
 Curve : elliptic.P224(), // this is just for example, change to suit your need
 X : x,
 Y : y,
  }


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

  fmt.Printf("Public key : %x \n", publickey)
 }

Sample output :

go run elliptic.go

Private key : &{a24435364e9885bf0c0f675b1fc413e95988765117c6f23a35285aa1}

Public key : &{{2082142d0 [15c1d21 3280d61 2112234 ....}

Reference :

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

Advertisement