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 :
Advertisement
Something interesting
Tutorials
+9.6k Golang : Copy map(hash table) example
+13.9k Golang : Get current time
+13k Golang : Calculate elapsed years or months since a date
+17.5k Golang : Find smallest number in array
+26.9k Golang : Force your program to run with root permissions
+16.4k Golang : Send email and SMTP configuration example
+18.7k Unmarshal/Load CSV record into struct in Go
+13.4k Golang : error parsing regexp: invalid or unsupported Perl syntax
+34.1k Golang : Create x509 certificate, private and public keys
+4.9k Python : Find out the variable type and determine the type with simple test
+12.2k Golang : Simple client-server HMAC authentication without SSL example
+7.1k Golang : Array mapping with Interface