Standard library / crypto/ecdh
crypto/ecdh.PrivateKey
Opaque secret key for ECDH; use curve constructors. Bytes returns secret encoding, PublicKey derives public share, ECDH computes secret. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
type PrivateKey struct {}Working Example
Example Snippetgo
package main
import ( "crypto/ecdh" "crypto/rand" "fmt")
func main() { k := must(ecdh.X25519().GenerateKey(rand.Reader)) peer := must(ecdh.X25519().GenerateKey(rand.Reader)) s := must(k.ECDH(peer.PublicKey())) fmt.Println(len(s) == 32)}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) }}