Standard library / crypto/ecdsa

crypto/ecdsa.GenerateKey()

Generates an ECDSA key pair on a supported curve with secure randomness. Prefer standard NIST curves. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error)

Parameters & Attributes

NameTypeDescription
celliptic.CurveCurve used for key generation.
randio.ReaderCryptographically secure random source; see description for exceptions.

Return Value

(*PrivateKey, error)

Result as described above; check any returned error before use.

Working Example

Example Snippetgo
package main
import (  "crypto/ecdsa"  "crypto/elliptic"  "crypto/rand"  "fmt")
func main() {  k := must(ecdsa.GenerateKey(elliptic.P256(), rand.Reader))  fmt.Println(k.Curve == elliptic.P256())}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference